blob: ff95eeaac33439b7eef438e6caef5e6a7167e544 [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
robertphillips5f2fa472016-05-19 11:36:25 -070035static sk_sp<const 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());
robertphillips5f2fa472016-05-19 11:36:25 -070045 return sk_sp<const GrFragmentProcessor>(GrTextureDomainEffect::Create(
46 result,
bsalomon0ba8c242015-10-07 09:20:28 -070047 mat,
48 GrTextureDomain::MakeTexelDomain(result, domainTexels),
49 GrTextureDomain::kDecal_Mode,
50 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070051 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000052}
53
robertphillips976f5f02016-06-03 10:59:20 -070054void GrClipMaskManager::DrawNonAARect(GrDrawContext* drawContext,
55 const GrFixedClip& clip,
56 const SkMatrix& viewMatrix,
57 const SkRect& rect,
58 bool doAA,
59 const GrUserStencilSettings* stencilSettings) {
60 drawContext->drawContextPriv().stencilRect(clip, stencilSettings, doAA, viewMatrix, rect);
joshualitta8b84992016-01-13 13:35:35 -080061}
62
robertphillips3f7357f2015-10-27 07:17:33 -070063// Does the path in 'element' require SW rendering? If so, return true (and,
64// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
65// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070066bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070067 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070068 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070069 const SkMatrix& viewMatrix,
70 const Element* element,
71 GrPathRenderer** prOut,
72 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070073 if (Element::kRect_Type == element->getType()) {
74 // rects can always be drawn directly w/o using the software path
75 // TODO: skip rrects once we're drawing them directly.
76 if (prOut) {
77 *prOut = nullptr;
78 }
79 return false;
80 } else {
81 // We shouldn't get here with an empty clip element.
82 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070083
robertphillips3f7357f2015-10-27 07:17:33 -070084 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
85 SkPath path;
86 element->asPath(&path);
87 if (path.isInverseFillType()) {
88 path.toggleInverseFillType();
89 }
halcanary9d524f22016-03-29 09:03:52 -070090
robertphillips3f7357f2015-10-27 07:17:33 -070091 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070092
robertphillips423e3372015-10-27 09:23:38 -070093 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070094 type = element->isAA()
95 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
96 : GrPathRendererChain::kStencilAndColor_DrawType;
97 } else {
98 type = element->isAA()
99 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700100 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700101 }
halcanary9d524f22016-03-29 09:03:52 -0700102
robertphillips68737822015-10-29 12:12:21 -0700103 GrPathRenderer::CanDrawPathArgs canDrawArgs;
104 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
105 canDrawArgs.fViewMatrix = &viewMatrix;
106 canDrawArgs.fPath = &path;
bsalomon6663acf2016-05-10 09:14:17 -0700107 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700108 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700109 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700110 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700111
robertphillips3f7357f2015-10-27 07:17:33 -0700112 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700113 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700114 if (prOut) {
115 *prOut = pr;
116 }
117 return SkToBool(!pr);
118 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000119}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000120
robertphillips@google.comfa662942012-05-17 12:20:22 +0000121/*
122 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
123 * will be used on any element. If so, it returns true to indicate that the
124 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
125 */
robertphillips391395d2016-03-02 09:26:36 -0800126bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
127 const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700128 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800129 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800130 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000131 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000132 // a clip gets complex enough it can just be done in SW regardless
133 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000134
joshualitt8059eb92014-12-29 15:10:07 -0800135 // Set the matrix so that rendered clip elements are transformed to mask space from clip
136 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700137 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800138
tfarinabf54e492014-10-23 17:47:18 -0700139 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000140 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700141
142 SkRegion::Op op = element->getOp();
143 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700144 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700145 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700146
cdalton93a379b2016-05-11 13:58:08 -0700147 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips976f5f02016-06-03 10:59:20 -0700148 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700149 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000150 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000152 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000153}
154
robertphillips976f5f02016-06-03 10:59:20 -0700155static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
156 bool abortIfAA,
157 SkVector& clipToRTOffset,
158 const SkRect* drawBounds,
159 sk_sp<const GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700161 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000162 boundsInClipSpace = *drawBounds;
163 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
164 }
bsalomon0ba8c242015-10-07 09:20:28 -0700165 SkASSERT(elements.count() <= kMaxAnalyticElements);
166 const GrFragmentProcessor* fps[kMaxAnalyticElements];
167 for (int i = 0; i < kMaxAnalyticElements; ++i) {
168 fps[i] = nullptr;
169 }
170 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700171 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700173 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000174 SkRegion::Op op = iter.get()->getOp();
175 bool invert;
176 bool skip = false;
177 switch (op) {
178 case SkRegion::kReplace_Op:
179 SkASSERT(iter.get() == elements.head());
180 // Fallthrough, handled same as intersect.
181 case SkRegion::kIntersect_Op:
182 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700183 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 skip = true;
185 }
186 break;
187 case SkRegion::kDifference_Op:
188 invert = true;
189 // We don't currently have a cheap test for whether a rect is fully outside an
190 // element's primitive, so don't attempt to set skip.
191 break;
192 default:
193 failed = true;
194 break;
195 }
196 if (failed) {
197 break;
198 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000199 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700200 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800201 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700202 if (abortIfAA) {
203 failed = true;
204 break;
205 }
joshualittb0a8a372014-09-23 09:50:21 -0700206 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700207 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700209 edgeType =
210 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 }
bsalomona912dde2015-10-14 15:01:50 -0700212
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 switch (iter.get()->getType()) {
214 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700215 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
216 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 break;
218 case SkClipStack::Element::kRRect_Type: {
219 SkRRect rrect = iter.get()->getRRect();
220 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700221 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 break;
223 }
224 case SkClipStack::Element::kRect_Type: {
225 SkRect rect = iter.get()->getRect();
226 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700227 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 break;
229 }
230 default:
231 break;
232 }
bsalomon0ba8c242015-10-07 09:20:28 -0700233 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 failed = true;
235 break;
236 }
bsalomon0ba8c242015-10-07 09:20:28 -0700237 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 }
mtklein217daa72014-07-02 12:55:21 -0700239 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
241
bsalomon0b5b6b22015-10-14 08:31:34 -0700242 *resultFP = nullptr;
243 if (!failed && fpCnt) {
robertphillips5f2fa472016-05-19 11:36:25 -0700244 resultFP->reset(GrFragmentProcessor::RunInSeries(fps, fpCnt));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000245 }
bsalomon0ba8c242015-10-07 09:20:28 -0700246 for (int i = 0; i < fpCnt; ++i) {
247 fps[i]->unref();
248 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700249 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000250}
251
robertphillips@google.comf294b772012-04-27 14:29:26 +0000252////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000253// sort out what kind of clip mask needs to be created: alpha, stencil,
254// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700255bool GrClipMaskManager::SetupClipping(GrContext* context,
256 const GrPipelineBuilder& pipelineBuilder,
257 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700258 const GrClipStackClip& clip,
bsalomon0ba8c242015-10-07 09:20:28 -0700259 const SkRect* devBounds,
260 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700261 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
262 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700263 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000264
bsalomonf045d602015-11-18 19:01:12 -0800265 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800266 int32_t genID = 0;
267 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000268 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800269 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000270
robertphillips976f5f02016-06-03 10:59:20 -0700271 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(drawContext->width(), drawContext->height());
cdalton846c0512016-05-13 10:25:00 -0700272 clipSpaceRTIBounds.offset(clip.origin());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273
cdalton846c0512016-05-13 10:25:00 -0700274 SkIRect clipSpaceReduceQueryBounds;
275#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
276 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
277 SkIRect devIBounds = devBounds->roundOut();
278 devIBounds.offset(clip.origin());
279 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800280 return false;
281 }
cdalton846c0512016-05-13 10:25:00 -0700282 } else {
283 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
bsalomon96e02a82015-03-06 07:13:01 -0800284 }
cdalton846c0512016-05-13 10:25:00 -0700285 GrReducedClip::ReduceClipStack(*clip.clipStack(),
286 clipSpaceReduceQueryBounds,
287 &elements,
288 &genID,
289 &initialState,
290 &clipSpaceIBounds,
291 &requiresAA);
292 if (elements.isEmpty()) {
293 if (GrReducedClip::kAllIn_InitialState == initialState) {
294 if (clipSpaceIBounds == clipSpaceRTIBounds) {
295 return true;
296 }
297 } else {
298 return false;
299 }
300 }
cdalton93a379b2016-05-11 13:58:08 -0700301
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000302 // An element count of 4 was chosen because of the common pattern in Blink of:
303 // isect RR
304 // diff RR
305 // isect convex_poly
306 // isect convex_poly
307 // when drawing rounded div borders. This could probably be tuned based on a
308 // configuration's relative costs of switching RTs to generate a mask vs
309 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700310 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800311 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
312 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800313 // When there are multiple samples we want to do per-sample clipping, not compute a
314 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700315 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
316 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700317 // With a single color sample, any coverage info is lost from color once it hits the
318 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
319 // is multisampled.
320 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700321 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700322 }
robertphillips5f2fa472016-05-19 11:36:25 -0700323 sk_sp<const GrFragmentProcessor> clipFP;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000324 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700325 (requiresAA &&
robertphillips976f5f02016-06-03 10:59:20 -0700326 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
327 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700328 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800329 scissorSpaceIBounds.offset(-clip.origin());
robertphillips976f5f02016-06-03 10:59:20 -0700330 if (!devBounds || !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
robertphillips5f2fa472016-05-19 11:36:25 -0700331 out->makeScissoredFPBased(clipFP, scissorSpaceIBounds);
332 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000333 }
robertphillips5f2fa472016-05-19 11:36:25 -0700334 out->makeFPBased(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000335 return true;
336 }
337 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000338
cdaltonede75742015-11-11 15:27:57 -0800339 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700340 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700341 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000342
joshualitt8059eb92014-12-29 15:10:07 -0800343 // The top-left of the mask corresponds to the top-left corner of the bounds.
344 SkVector clipToMaskOffset = {
345 SkIntToScalar(-clipSpaceIBounds.fLeft),
346 SkIntToScalar(-clipSpaceIBounds.fTop)
347 };
348
robertphillips976f5f02016-06-03 10:59:20 -0700349 if (UseSWOnlyPath(context, pipelineBuilder, drawContext,
350 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000351 // The clip geometry is complex enough that it will be more efficient to create it
352 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700353 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700354 genID,
355 initialState,
356 elements,
357 clipToMaskOffset,
358 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000359 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700360 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700361 genID,
362 initialState,
363 elements,
364 clipToMaskOffset,
365 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800366 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700367 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368 }
369
bsalomon49f085d2014-09-05 13:34:00 -0700370 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371 // The mask's top left coord should be pinned to the rounded-out top left corner of
372 // clipSpace bounds. We determine the mask's position WRT to the render target here.
373 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800374 rtSpaceMaskBounds.offset(-clip.origin());
robertphillips5f2fa472016-05-19 11:36:25 -0700375 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376 return true;
377 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000378 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000381 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800382 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700383 CreateStencilClipMask(context,
384 drawContext,
385 genID,
386 initialState,
387 elements,
388 clipSpaceIBounds,
389 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000390
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000391 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
392 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
393 // use both stencil and scissor test to the bounds for the final draw.
394 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
395 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
robertphillips5f2fa472016-05-19 11:36:25 -0700396 out->makeScissoredStencil(true, scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000397 return true;
398}
399
robertphillips391395d2016-03-02 09:26:36 -0800400static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700401 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700402 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800403 const SkMatrix& viewMatrix,
404 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800405
406 // TODO: Draw rrects directly here.
407 switch (element->getType()) {
408 case Element::kEmpty_Type:
409 SkDEBUGFAIL("Should never get here with an empty element.");
410 break;
robertphillips391395d2016-03-02 09:26:36 -0800411 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700412 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800413 element->getOp(),
414 element->isInverseFilled(),
415 element->isAA(),
416 viewMatrix, element->getRect());
417 break;
robertphillips86c60752016-03-02 08:43:13 -0800418 default: {
419 SkPath path;
420 element->asPath(&path);
421 if (path.isInverseFillType()) {
422 path.toggleInverseFillType();
423 }
424
cdalton862cff32016-05-12 15:09:48 -0700425 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800426 element->getOp(),
427 element->isInverseFilled(),
428 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800429 break;
430 }
431 }
robertphillips391395d2016-03-02 09:26:36 -0800432
433 return false;
434}
435
436static void draw_element(GrDrawContext* dc,
437 const GrClip& clip, // TODO: can this just always be WideOpen?
438 const GrPaint &paint,
439 const SkMatrix& viewMatrix,
440 const SkClipStack::Element* element) {
441
442 // TODO: Draw rrects directly here.
443 switch (element->getType()) {
444 case Element::kEmpty_Type:
445 SkDEBUGFAIL("Should never get here with an empty element.");
446 break;
447 case Element::kRect_Type:
448 dc->drawRect(clip, paint, viewMatrix, element->getRect());
449 break;
450 default: {
451 SkPath path;
452 element->asPath(&path);
453 if (path.isInverseFillType()) {
454 path.toggleInverseFillType();
455 }
456
bsalomon6663acf2016-05-10 09:14:17 -0700457 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800458 break;
459 }
460 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000461}
462
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000463////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700464// Create a 8-bit clip mask in alpha
465
466static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
467 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
468 GrUniqueKey::Builder builder(key, kDomain, 3);
469 builder[0] = clipGenID;
470 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
471 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
472}
473
robertphillipsc99b8f02016-05-15 07:53:35 -0700474sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
475 int32_t elementsGenID,
476 GrReducedClip::InitialState initialState,
477 const GrReducedClip::ElementList& elements,
478 const SkVector& clipToMaskOffset,
479 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800480 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700481 GrUniqueKey key;
482 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
483 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700484 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000485 }
486
robertphillips544b9aa2015-10-28 11:01:41 -0700487 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700488 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800489 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700490 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800491 }
492
robertphillipsc99b8f02016-05-15 07:53:35 -0700493 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
494 clipSpaceIBounds.width(),
495 clipSpaceIBounds.height(),
496 config));
robertphillips391395d2016-03-02 09:26:36 -0800497 if (!dc) {
498 return nullptr;
499 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700500
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 // The texture may be larger than necessary, this rect represents the part of the texture
502 // we populate with a rasterization of the clip.
503 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
504
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000505 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
506 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800507 dc->clear(&maskSpaceIBounds,
508 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
509 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000510
robertphillips391395d2016-03-02 09:26:36 -0800511 // Set the matrix so that rendered clip elements are transformed to mask space from clip
512 // space.
513 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
514
515 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000516 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
517 // pass must not set values outside of this bounds or stencil values outside the rect won't be
518 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800519
robertphillips@google.comf294b772012-04-27 14:29:26 +0000520 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700521 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000523 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000524 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000525 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700526 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700527
robertphillips391395d2016-03-02 09:26:36 -0800528 // draw directly into the result with the stencil set to make the pixels affected
529 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700530 static constexpr GrUserStencilSettings kStencilInElement(
531 GrUserStencilSettings::StaticInit<
532 0xffff,
533 GrUserStencilTest::kAlways,
534 0xffff,
535 GrUserStencilOp::kReplace,
536 GrUserStencilOp::kReplace,
537 0xffff>()
538 );
cdalton862cff32016-05-12 15:09:48 -0700539 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800540 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800541 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000542 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000543
robertphillips391395d2016-03-02 09:26:36 -0800544 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700545 static constexpr GrUserStencilSettings kDrawOutsideElement(
546 GrUserStencilSettings::StaticInit<
547 0x0000,
548 GrUserStencilTest::kEqual,
549 0xffff,
550 GrUserStencilOp::kZero,
551 GrUserStencilOp::kZero,
552 0xffff>()
553 );
cdalton862cff32016-05-12 15:09:48 -0700554 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800555 op, !invert, false,
556 translate,
557 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800558 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000559 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800561 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800562 GrPaint paint;
563 paint.setAntiAlias(element->isAA());
564 paint.setCoverageSetOpXPFactory(op, false);
565
cdalton846c0512016-05-13 10:25:00 -0700566 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567 }
568 }
569
robertphillipsc99b8f02016-05-15 07:53:35 -0700570 sk_sp<GrTexture> texture(dc->asTexture());
571 SkASSERT(texture);
572 texture->resourcePriv().setUniqueKey(key);
573 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574}
575
576////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000577// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000578// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700579bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
580 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800581 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700582 GrReducedClip::InitialState initialState,
583 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000584 const SkIRect& clipSpaceIBounds,
585 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700586 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587
robertphillips976f5f02016-06-03 10:59:20 -0700588 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
589 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700590 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591 return false;
592 }
593
robertphillips976f5f02016-06-03 10:59:20 -0700594 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700595 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
596 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000597 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
598 SkVector translate = {
599 SkIntToScalar(clipSpaceToStencilOffset.fX),
600 SkIntToScalar(clipSpaceToStencilOffset.fY)
601 };
joshualitt8059eb92014-12-29 15:10:07 -0800602 SkMatrix viewMatrix;
603 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604
bsalomon@google.com9f131742012-12-13 20:43:56 +0000605 // We set the current clip to the bounds so that our recursive draws are scissored to them.
606 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
607 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700608 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
robertphillips976f5f02016-06-03 10:59:20 -0700610 drawContext->drawContextPriv().clearStencilClip(
611 stencilSpaceIBounds,
612 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000613
614 // walk through each clip element and perform its set op
615 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700616 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000617 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800618
tomhudson@google.com8afae612012-08-14 15:03:35 +0000619 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700621 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000623 // This will be used to determine whether the clip shape can be rendered into the
624 // stencil with arbitrary stencil settings.
625 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000626
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000627 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
halcanary96fcdcc2015-08-27 07:41:13 -0700629 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000630 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000631 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000632 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000634 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000635 element->asPath(&clipPath);
636 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000637 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000638 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000639 }
robertphillips68737822015-10-29 12:12:21 -0700640
robertphillips68737822015-10-29 12:12:21 -0700641 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700642 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700643 canDrawArgs.fViewMatrix = &viewMatrix;
644 canDrawArgs.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700645 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700646 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700647 canDrawArgs.fHasUserStencilSettings = false;
648 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700649
robertphillips976f5f02016-06-03 10:59:20 -0700650 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
651 GrPathRendererChain::kStencilOnly_DrawType,
652 &stencilSupport);
653 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654 return false;
655 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 }
657
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000658 bool canRenderDirectToStencil =
659 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700660 bool drawDirectToClip; // Given the renderer, the element,
661 // fill rule, and set operation should
662 // we render the element directly to
663 // stencil bit used for clipping.
664 GrUserStencilSettings const* const* stencilPasses =
665 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
666 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667
668 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700669 if (!drawDirectToClip) {
670 static constexpr GrUserStencilSettings kDrawToStencil(
671 GrUserStencilSettings::StaticInit<
672 0x0000,
673 GrUserStencilTest::kAlways,
674 0xffff,
675 GrUserStencilOp::kIncMaybeClamp,
676 GrUserStencilOp::kIncMaybeClamp,
677 0xffff>()
678 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000679 if (Element::kRect_Type == element->getType()) {
robertphillips976f5f02016-06-03 10:59:20 -0700680 DrawNonAARect(drawContext, clip, viewMatrix,
681 element->getRect(), element->isAA(), &kDrawToStencil);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000683 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000684 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700685 GrPaint paint;
686 paint.setXPFactory(GrDisableColorXPFactory::Create());
687 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700688
689 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700690 args.fResourceProvider = context->resourceProvider();
691 args.fPaint = &paint;
692 args.fUserStencilSettings = &kDrawToStencil;
693 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700694 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700695 args.fColor = GrColor_WHITE;
696 args.fViewMatrix = &viewMatrix;
697 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700698 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700699 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700700 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700701 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000702 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700703 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700704 args.fResourceProvider = context->resourceProvider();
705 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700706 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700707 args.fViewMatrix = &viewMatrix;
708 args.fPath = &clipPath;
robertphillips976f5f02016-06-03 10:59:20 -0700709 args.fIsAA = element->isAA();
bsalomon0aff2fa2015-07-31 06:48:27 -0700710 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000711 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 }
713 }
714 }
715
716 // now we modify the clip bit by rendering either the clip
717 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700718 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700719 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800720
cdalton93a379b2016-05-11 13:58:08 -0700721 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000722 if (Element::kRect_Type == element->getType()) {
robertphillips976f5f02016-06-03 10:59:20 -0700723 DrawNonAARect(drawContext, clip,
724 viewMatrix, element->getRect(), element->isAA(), *pass);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700726 GrPaint paint;
727 paint.setXPFactory(GrDisableColorXPFactory::Create());
728 paint.setAntiAlias(element->isAA());
729
bsalomon0aff2fa2015-07-31 06:48:27 -0700730 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700731 args.fResourceProvider = context->resourceProvider();
732 args.fPaint = &paint;
733 args.fUserStencilSettings = *pass;
734 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700735 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700736 args.fColor = GrColor_WHITE;
737 args.fViewMatrix = &viewMatrix;
738 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700739 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700740 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700741 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700742 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 }
744 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000745 // The view matrix is setup to do clip space -> stencil space translation, so
746 // draw rect in clip space.
robertphillips976f5f02016-06-03 10:59:20 -0700747 DrawNonAARect(drawContext, clip, viewMatrix,
748 SkRect::Make(clipSpaceIBounds), false, *pass);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 }
750 }
751 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 return true;
754}
755
bsalomon@google.com411dad02012-06-05 20:24:20 +0000756////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700757sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700758 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700759 int32_t elementsGenID,
760 GrReducedClip::InitialState initialState,
761 const GrReducedClip::ElementList& elements,
762 const SkVector& clipToMaskOffset,
763 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700764 GrUniqueKey key;
765 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700766 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700767 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000768 }
769
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000770 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
771 // the top left corner of the resulting rect to the top left of the texture.
772 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
773
robertphillips0152d732016-05-20 06:38:43 -0700774 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000775
joshualitt8059eb92014-12-29 15:10:07 -0800776 // Set the matrix so that rendered clip elements are transformed to mask space from clip
777 // space.
778 SkMatrix translate;
779 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800780
robertphillips98377402016-05-13 05:47:23 -0700781 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700782 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000783
tfarinabf54e492014-10-23 17:47:18 -0700784 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000785 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000786 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000787
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000788 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
789 // Intersect and reverse difference require modifying pixels outside of the geometry
790 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
791 // but leave the pixels inside the geometry alone. For reverse difference we invert all
792 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000793 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000794 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000795 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700796 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000797 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000798 SkPath clipPath;
799 element->asPath(&clipPath);
800 clipPath.toggleInverseFillType();
robertphillips98377402016-05-13 05:47:23 -0700801 helper.drawPath(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op,
802 element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000803 continue;
804 }
805
806 // The other ops (union, xor, diff) only affect pixels inside
807 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000808 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700809 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000810 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000811 SkPath path;
812 element->asPath(&path);
robertphillips98377402016-05-13 05:47:23 -0700813 helper.drawPath(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000814 }
815 }
816
krajcevskiad1dc582014-06-10 15:06:47 -0700817 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800818 GrSurfaceDesc desc;
819 desc.fWidth = clipSpaceIBounds.width();
820 desc.fHeight = clipSpaceIBounds.height();
821 desc.fConfig = kAlpha_8_GrPixelConfig;
822
robertphillips0152d732016-05-20 06:38:43 -0700823 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800824 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700825 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700826 }
robertphillips391395d2016-03-02 09:26:36 -0800827 result->resourcePriv().setUniqueKey(key);
828
robertphillipsc99b8f02016-05-15 07:53:35 -0700829 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000830
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000831 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000832}