blob: 81b6fe861435138cc34267d53626bd8cca56c691 [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"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawTarget.h"
bsalomon473addf2015-10-02 07:49:05 -070013#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrPaint.h"
15#include "GrPathRenderer.h"
16#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070018#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070019#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070022#include "SkTLazy.h"
joshualitta8b84992016-01-13 13:35:35 -080023#include "batches/GrRectBatchFactory.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080026#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080027#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
31////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
robertphillips5f2fa472016-05-19 11:36:25 -070034static sk_sp<const GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
35 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000036 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080037 // We use device coords to compute the texture coordinates. We set our matrix to be a
38 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000043 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
robertphillips5f2fa472016-05-19 11:36:25 -070044 return sk_sp<const GrFragmentProcessor>(GrTextureDomainEffect::Create(
45 result,
bsalomon0ba8c242015-10-07 09:20:28 -070046 mat,
47 GrTextureDomain::MakeTexelDomain(result, domainTexels),
48 GrTextureDomain::kDecal_Mode,
49 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070050 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000051}
52
joshualitta8b84992016-01-13 13:35:35 -080053static void draw_non_aa_rect(GrDrawTarget* drawTarget,
54 const GrPipelineBuilder& pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -070055 const GrClip& clip,
joshualitta8b84992016-01-13 13:35:35 -080056 GrColor color,
57 const SkMatrix& viewMatrix,
58 const SkRect& rect) {
59 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
60 nullptr, nullptr));
cdalton862cff32016-05-12 15:09:48 -070061 drawTarget->drawBatch(pipelineBuilder, clip, batch);
joshualitta8b84992016-01-13 13:35:35 -080062}
63
robertphillips3f7357f2015-10-27 07:17:33 -070064// Does the path in 'element' require SW rendering? If so, return true (and,
65// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
66// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070067bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070068 bool hasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -070069 const GrRenderTarget* rt,
70 const SkMatrix& viewMatrix,
71 const Element* element,
72 GrPathRenderer** prOut,
73 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070074 if (Element::kRect_Type == element->getType()) {
75 // rects can always be drawn directly w/o using the software path
76 // TODO: skip rrects once we're drawing them directly.
77 if (prOut) {
78 *prOut = nullptr;
79 }
80 return false;
81 } else {
82 // We shouldn't get here with an empty clip element.
83 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070084
robertphillips3f7357f2015-10-27 07:17:33 -070085 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
86 SkPath path;
87 element->asPath(&path);
88 if (path.isInverseFillType()) {
89 path.toggleInverseFillType();
90 }
halcanary9d524f22016-03-29 09:03:52 -070091
robertphillips3f7357f2015-10-27 07:17:33 -070092 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070093
robertphillips423e3372015-10-27 09:23:38 -070094 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070095 type = element->isAA()
96 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
97 : GrPathRendererChain::kStencilAndColor_DrawType;
98 } else {
99 type = element->isAA()
100 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700101 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700102 }
halcanary9d524f22016-03-29 09:03:52 -0700103
robertphillips68737822015-10-29 12:12:21 -0700104 GrPathRenderer::CanDrawPathArgs canDrawArgs;
105 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
106 canDrawArgs.fViewMatrix = &viewMatrix;
107 canDrawArgs.fPath = &path;
bsalomon6663acf2016-05-10 09:14:17 -0700108 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700109 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700110 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700111 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
112
robertphillips3f7357f2015-10-27 07:17:33 -0700113 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700114 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700115 if (prOut) {
116 *prOut = pr;
117 }
118 return SkToBool(!pr);
119 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000120}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000121
robertphillips423e3372015-10-27 09:23:38 -0700122// Determines whether it is possible to draw the element to both the stencil buffer and the
123// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
124// also returned.
robertphillips68737822015-10-29 12:12:21 -0700125GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context,
126 GrTexture* texture,
127 const SkMatrix& viewMatrix,
128 const SkClipStack::Element* element) {
robertphillips544b9aa2015-10-28 11:01:41 -0700129 GrPathRenderer* pr;
cdalton93a379b2016-05-11 13:58:08 -0700130 constexpr bool kNeedsStencil = true;
131 constexpr bool kHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -0700132 PathNeedsSWRenderer(context,
cdalton93a379b2016-05-11 13:58:08 -0700133 kHasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -0700134 texture->asRenderTarget(),
135 viewMatrix,
136 element,
137 &pr,
138 kNeedsStencil);
robertphillips544b9aa2015-10-28 11:01:41 -0700139 return pr;
robertphillips423e3372015-10-27 09:23:38 -0700140}
141
robertphillips544b9aa2015-10-28 11:01:41 -0700142GrContext* GrClipMaskManager::getContext() {
143 return fDrawTarget->cmmAccess().context();
144}
bsalomonedd77a12015-05-29 09:45:57 -0700145
robertphillips544b9aa2015-10-28 11:01:41 -0700146const GrCaps* GrClipMaskManager::caps() const {
147 return fDrawTarget->caps();
148}
149
150GrResourceProvider* GrClipMaskManager::resourceProvider() {
151 return fDrawTarget->cmmAccess().resourceProvider();
152}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000153/*
154 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
155 * will be used on any element. If so, it returns true to indicate that the
156 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
157 */
robertphillips391395d2016-03-02 09:26:36 -0800158bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
159 const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700160 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800161 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800162 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000163 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000164 // a clip gets complex enough it can just be done in SW regardless
165 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000166
joshualitt8059eb92014-12-29 15:10:07 -0800167 // Set the matrix so that rendered clip elements are transformed to mask space from clip
168 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700169 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800170
tfarinabf54e492014-10-23 17:47:18 -0700171 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000172 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700173
174 SkRegion::Op op = element->getOp();
175 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700176 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700177 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700178
cdalton93a379b2016-05-11 13:58:08 -0700179 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips68737822015-10-29 12:12:21 -0700180 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700181 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000182 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000183 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000184 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000185}
186
bsalomon0b5b6b22015-10-14 08:31:34 -0700187bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700188 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700189 SkVector& clipToRTOffset,
190 const SkRect* drawBounds,
robertphillips5f2fa472016-05-19 11:36:25 -0700191 sk_sp<const GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700193 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 boundsInClipSpace = *drawBounds;
195 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
196 }
bsalomon0ba8c242015-10-07 09:20:28 -0700197 SkASSERT(elements.count() <= kMaxAnalyticElements);
198 const GrFragmentProcessor* fps[kMaxAnalyticElements];
199 for (int i = 0; i < kMaxAnalyticElements; ++i) {
200 fps[i] = nullptr;
201 }
202 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700203 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700205 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 SkRegion::Op op = iter.get()->getOp();
207 bool invert;
208 bool skip = false;
209 switch (op) {
210 case SkRegion::kReplace_Op:
211 SkASSERT(iter.get() == elements.head());
212 // Fallthrough, handled same as intersect.
213 case SkRegion::kIntersect_Op:
214 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700215 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 skip = true;
217 }
218 break;
219 case SkRegion::kDifference_Op:
220 invert = true;
221 // We don't currently have a cheap test for whether a rect is fully outside an
222 // element's primitive, so don't attempt to set skip.
223 break;
224 default:
225 failed = true;
226 break;
227 }
228 if (failed) {
229 break;
230 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700232 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800233 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700234 if (abortIfAA) {
235 failed = true;
236 break;
237 }
joshualittb0a8a372014-09-23 09:50:21 -0700238 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700239 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700241 edgeType =
242 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 }
bsalomona912dde2015-10-14 15:01:50 -0700244
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000245 switch (iter.get()->getType()) {
246 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700247 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
248 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000249 break;
250 case SkClipStack::Element::kRRect_Type: {
251 SkRRect rrect = iter.get()->getRRect();
252 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700253 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000254 break;
255 }
256 case SkClipStack::Element::kRect_Type: {
257 SkRect rect = iter.get()->getRect();
258 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700259 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000260 break;
261 }
262 default:
263 break;
264 }
bsalomon0ba8c242015-10-07 09:20:28 -0700265 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000266 failed = true;
267 break;
268 }
bsalomon0ba8c242015-10-07 09:20:28 -0700269 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 }
mtklein217daa72014-07-02 12:55:21 -0700271 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 }
273
bsalomon0b5b6b22015-10-14 08:31:34 -0700274 *resultFP = nullptr;
275 if (!failed && fpCnt) {
robertphillips5f2fa472016-05-19 11:36:25 -0700276 resultFP->reset(GrFragmentProcessor::RunInSeries(fps, fpCnt));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000277 }
bsalomon0ba8c242015-10-07 09:20:28 -0700278 for (int i = 0; i < fpCnt; ++i) {
279 fps[i]->unref();
280 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700281 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000282}
283
robertphillips@google.comf294b772012-04-27 14:29:26 +0000284////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000285// sort out what kind of clip mask needs to be created: alpha, stencil,
286// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700287bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
cdalton846c0512016-05-13 10:25:00 -0700288 const GrClipStackClip& clip,
bsalomon0ba8c242015-10-07 09:20:28 -0700289 const SkRect* devBounds,
290 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700291 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
292 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700293 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000294
bsalomonf045d602015-11-18 19:01:12 -0800295 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800296 int32_t genID = 0;
297 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000298 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800299 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700300 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700303 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000304
joshualitt44701df2015-02-23 14:44:57 -0800305 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
cdalton846c0512016-05-13 10:25:00 -0700306 clipSpaceRTIBounds.offset(clip.origin());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307
cdalton846c0512016-05-13 10:25:00 -0700308 SkIRect clipSpaceReduceQueryBounds;
309#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
310 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
311 SkIRect devIBounds = devBounds->roundOut();
312 devIBounds.offset(clip.origin());
313 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800314 return false;
315 }
cdalton846c0512016-05-13 10:25:00 -0700316 } else {
317 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
bsalomon96e02a82015-03-06 07:13:01 -0800318 }
cdalton846c0512016-05-13 10:25:00 -0700319 GrReducedClip::ReduceClipStack(*clip.clipStack(),
320 clipSpaceReduceQueryBounds,
321 &elements,
322 &genID,
323 &initialState,
324 &clipSpaceIBounds,
325 &requiresAA);
326 if (elements.isEmpty()) {
327 if (GrReducedClip::kAllIn_InitialState == initialState) {
328 if (clipSpaceIBounds == clipSpaceRTIBounds) {
329 return true;
330 }
331 } else {
332 return false;
333 }
334 }
cdalton93a379b2016-05-11 13:58:08 -0700335
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000336 // An element count of 4 was chosen because of the common pattern in Blink of:
337 // isect RR
338 // diff RR
339 // isect convex_poly
340 // isect convex_poly
341 // when drawing rounded div borders. This could probably be tuned based on a
342 // configuration's relative costs of switching RTs to generate a mask vs
343 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700344 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800345 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
346 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800347 // When there are multiple samples we want to do per-sample clipping, not compute a
348 // fractional pixel coverage.
cdalton3ccf2e72016-05-06 09:41:16 -0700349 bool disallowAnalyticAA = rt->isStencilBufferMultisampled();
350 if (disallowAnalyticAA && !rt->numColorSamples()) {
351 // With a single color sample, any coverage info is lost from color once it hits the
352 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
353 // is multisampled.
354 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700355 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700356 }
robertphillips5f2fa472016-05-19 11:36:25 -0700357 sk_sp<const GrFragmentProcessor> clipFP;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000358 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700359 (requiresAA &&
360 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
361 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700362 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800363 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700364 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700365 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
robertphillips5f2fa472016-05-19 11:36:25 -0700366 out->makeScissoredFPBased(clipFP, scissorSpaceIBounds);
367 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000368 }
robertphillips5f2fa472016-05-19 11:36:25 -0700369 out->makeFPBased(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000370 return true;
371 }
372 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000373
cdaltonede75742015-11-11 15:27:57 -0800374 // If the stencil buffer is multisampled we can use it to do everything.
375 if (!rt->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700376 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000377
joshualitt8059eb92014-12-29 15:10:07 -0800378 // The top-left of the mask corresponds to the top-left corner of the bounds.
379 SkVector clipToMaskOffset = {
380 SkIntToScalar(-clipSpaceIBounds.fLeft),
381 SkIntToScalar(-clipSpaceIBounds.fTop)
382 };
383
robertphillips391395d2016-03-02 09:26:36 -0800384 if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000385 // The clip geometry is complex enough that it will be more efficient to create it
386 // entirely in software
robertphillipsc99b8f02016-05-15 07:53:35 -0700387 result = CreateSoftwareClipMask(this->getContext(),
388 genID,
389 initialState,
390 elements,
391 clipToMaskOffset,
392 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000393 } else {
robertphillipsc99b8f02016-05-15 07:53:35 -0700394 result = CreateAlphaClipMask(this->getContext(),
395 genID,
396 initialState,
397 elements,
398 clipToMaskOffset,
399 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800400 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700401 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 }
403
bsalomon49f085d2014-09-05 13:34:00 -0700404 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 // The mask's top left coord should be pinned to the rounded-out top left corner of
406 // clipSpace bounds. We determine the mask's position WRT to the render target here.
407 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800408 rtSpaceMaskBounds.offset(-clip.origin());
robertphillips5f2fa472016-05-19 11:36:25 -0700409 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 return true;
411 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000415 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800416 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800417 this->createStencilClipMask(rt,
418 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000419 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 elements,
421 clipSpaceIBounds,
422 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000423
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
425 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
426 // use both stencil and scissor test to the bounds for the final draw.
427 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
428 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
robertphillips5f2fa472016-05-19 11:36:25 -0700429 out->makeScissoredStencil(true, scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000430 return true;
431}
432
robertphillips391395d2016-03-02 09:26:36 -0800433static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700434 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700435 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800436 const SkMatrix& viewMatrix,
437 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800438
439 // TODO: Draw rrects directly here.
440 switch (element->getType()) {
441 case Element::kEmpty_Type:
442 SkDEBUGFAIL("Should never get here with an empty element.");
443 break;
robertphillips391395d2016-03-02 09:26:36 -0800444 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700445 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800446 element->getOp(),
447 element->isInverseFilled(),
448 element->isAA(),
449 viewMatrix, element->getRect());
450 break;
robertphillips86c60752016-03-02 08:43:13 -0800451 default: {
452 SkPath path;
453 element->asPath(&path);
454 if (path.isInverseFillType()) {
455 path.toggleInverseFillType();
456 }
457
cdalton862cff32016-05-12 15:09:48 -0700458 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800459 element->getOp(),
460 element->isInverseFilled(),
461 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800462 break;
463 }
464 }
robertphillips391395d2016-03-02 09:26:36 -0800465
466 return false;
467}
468
469static void draw_element(GrDrawContext* dc,
470 const GrClip& clip, // TODO: can this just always be WideOpen?
471 const GrPaint &paint,
472 const SkMatrix& viewMatrix,
473 const SkClipStack::Element* element) {
474
475 // TODO: Draw rrects directly here.
476 switch (element->getType()) {
477 case Element::kEmpty_Type:
478 SkDEBUGFAIL("Should never get here with an empty element.");
479 break;
480 case Element::kRect_Type:
481 dc->drawRect(clip, paint, viewMatrix, element->getRect());
482 break;
483 default: {
484 SkPath path;
485 element->asPath(&path);
486 if (path.isInverseFillType()) {
487 path.toggleInverseFillType();
488 }
489
bsalomon6663acf2016-05-10 09:14:17 -0700490 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800491 break;
492 }
493 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000494}
495
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700497// Create a 8-bit clip mask in alpha
498
499static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
500 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
501 GrUniqueKey::Builder builder(key, kDomain, 3);
502 builder[0] = clipGenID;
503 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
504 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
505}
506
robertphillipsc99b8f02016-05-15 07:53:35 -0700507sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
508 int32_t elementsGenID,
509 GrReducedClip::InitialState initialState,
510 const GrReducedClip::ElementList& elements,
511 const SkVector& clipToMaskOffset,
512 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800513 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700514 GrUniqueKey key;
515 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
516 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700517 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000518 }
519
robertphillips544b9aa2015-10-28 11:01:41 -0700520 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700521 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800522 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700523 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800524 }
525
robertphillipsc99b8f02016-05-15 07:53:35 -0700526 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
527 clipSpaceIBounds.width(),
528 clipSpaceIBounds.height(),
529 config));
robertphillips391395d2016-03-02 09:26:36 -0800530 if (!dc) {
531 return nullptr;
532 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700533
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 // The texture may be larger than necessary, this rect represents the part of the texture
535 // we populate with a rasterization of the clip.
536 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
537
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000538 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
539 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800540 dc->clear(&maskSpaceIBounds,
541 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
542 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000543
robertphillips391395d2016-03-02 09:26:36 -0800544 // Set the matrix so that rendered clip elements are transformed to mask space from clip
545 // space.
546 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
547
548 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000549 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
550 // pass must not set values outside of this bounds or stencil values outside the rect won't be
551 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800552
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700554 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000555 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000556 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000557 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000558 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700559 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700560
robertphillips391395d2016-03-02 09:26:36 -0800561 // draw directly into the result with the stencil set to make the pixels affected
562 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700563 static constexpr GrUserStencilSettings kStencilInElement(
564 GrUserStencilSettings::StaticInit<
565 0xffff,
566 GrUserStencilTest::kAlways,
567 0xffff,
568 GrUserStencilOp::kReplace,
569 GrUserStencilOp::kReplace,
570 0xffff>()
571 );
cdalton862cff32016-05-12 15:09:48 -0700572 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800573 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800574 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000575 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000576
robertphillips391395d2016-03-02 09:26:36 -0800577 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700578 static constexpr GrUserStencilSettings kDrawOutsideElement(
579 GrUserStencilSettings::StaticInit<
580 0x0000,
581 GrUserStencilTest::kEqual,
582 0xffff,
583 GrUserStencilOp::kZero,
584 GrUserStencilOp::kZero,
585 0xffff>()
586 );
cdalton862cff32016-05-12 15:09:48 -0700587 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800588 op, !invert, false,
589 translate,
590 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800591 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000592 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800594 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800595 GrPaint paint;
596 paint.setAntiAlias(element->isAA());
597 paint.setCoverageSetOpXPFactory(op, false);
598
cdalton846c0512016-05-13 10:25:00 -0700599 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600 }
601 }
602
robertphillipsc99b8f02016-05-15 07:53:35 -0700603 sk_sp<GrTexture> texture(dc->asTexture());
604 SkASSERT(texture);
605 texture->resourcePriv().setUniqueKey(key);
606 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607}
608
609////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000610// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000611// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800612bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
613 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700614 GrReducedClip::InitialState initialState,
615 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 const SkIRect& clipSpaceIBounds,
617 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700618 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000619
robertphillips544b9aa2015-10-28 11:01:41 -0700620 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700621 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622 return false;
623 }
624
egdaniel8dc7c3a2015-04-16 11:22:42 -0700625 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
626 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000627 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
628 SkVector translate = {
629 SkIntToScalar(clipSpaceToStencilOffset.fX),
630 SkIntToScalar(clipSpaceToStencilOffset.fY)
631 };
joshualitt8059eb92014-12-29 15:10:07 -0800632 SkMatrix viewMatrix;
633 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000634
bsalomon@google.com9f131742012-12-13 20:43:56 +0000635 // We set the current clip to the bounds so that our recursive draws are scissored to them.
636 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
637 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700638 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639
bsalomonb3b9aec2015-09-10 11:16:35 -0700640 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
641 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642
643 // walk through each clip element and perform its set op
644 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700645 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000646 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800647
egdaniel8dd688b2015-01-22 10:16:09 -0800648 GrPipelineBuilder pipelineBuilder;
649 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800650
egdaniel8dd688b2015-01-22 10:16:09 -0800651 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800652
653 // if the target is MSAA then we want MSAA enabled when the clip is soft
cdaltonede75742015-11-11 15:27:57 -0800654 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700655 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800656 }
657
tomhudson@google.com8afae612012-08-14 15:03:35 +0000658 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700660 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000662 // This will be used to determine whether the clip shape can be rendered into the
663 // stencil with arbitrary stencil settings.
664 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000666 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
halcanary96fcdcc2015-08-27 07:41:13 -0700668 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000669 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000670 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000671 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000673 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000674 element->asPath(&clipPath);
675 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000676 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000677 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000678 }
robertphillips68737822015-10-29 12:12:21 -0700679
cdalton93a379b2016-05-11 13:58:08 -0700680 SkASSERT(!pipelineBuilder.hasUserStencilSettings());
robertphillips68737822015-10-29 12:12:21 -0700681
682 GrPathRenderer::CanDrawPathArgs canDrawArgs;
683 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
684 canDrawArgs.fViewMatrix = &viewMatrix;
685 canDrawArgs.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700686 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700687 canDrawArgs.fAntiAlias = false;
cdalton93a379b2016-05-11 13:58:08 -0700688 canDrawArgs.fHasUserStencilSettings = pipelineBuilder.hasUserStencilSettings();
robertphillips68737822015-10-29 12:12:21 -0700689 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
690
691 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
692 GrPathRendererChain::kStencilOnly_DrawType,
693 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700694 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695 return false;
696 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 }
698
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000699 bool canRenderDirectToStencil =
700 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700701 bool drawDirectToClip; // Given the renderer, the element,
702 // fill rule, and set operation should
703 // we render the element directly to
704 // stencil bit used for clipping.
705 GrUserStencilSettings const* const* stencilPasses =
706 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
707 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
709 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700710 if (!drawDirectToClip) {
711 static constexpr GrUserStencilSettings kDrawToStencil(
712 GrUserStencilSettings::StaticInit<
713 0x0000,
714 GrUserStencilTest::kAlways,
715 0xffff,
716 GrUserStencilOp::kIncMaybeClamp,
717 GrUserStencilOp::kIncMaybeClamp,
718 0xffff>()
719 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000720 if (Element::kRect_Type == element->getType()) {
cdalton93a379b2016-05-11 13:58:08 -0700721 pipelineBuilder.setUserStencil(&kDrawToStencil);
joshualitt73bb4562015-03-25 07:16:21 -0700722
cdalton862cff32016-05-12 15:09:48 -0700723 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix,
joshualitta8b84992016-01-13 13:35:35 -0800724 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000726 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000727 if (canRenderDirectToStencil) {
cdalton93a379b2016-05-11 13:58:08 -0700728 pipelineBuilder.setUserStencil(&kDrawToStencil);
bsalomon0aff2fa2015-07-31 06:48:27 -0700729
730 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700731 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700732 args.fResourceProvider = this->getContext()->resourceProvider();
733 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700734 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700735 args.fColor = GrColor_WHITE;
736 args.fViewMatrix = &viewMatrix;
737 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700738 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700739 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700740 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700741 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000742 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700743 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700744 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700745 args.fResourceProvider = this->getContext()->resourceProvider();
746 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700747 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700748 args.fViewMatrix = &viewMatrix;
749 args.fPath = &clipPath;
bsalomon0aff2fa2015-07-31 06:48:27 -0700750 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000751 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 }
753 }
754 }
755
756 // now we modify the clip bit by rendering either the clip
757 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700758 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700759 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
760 pipelineBuilder.setUserStencil(*pass);
joshualitt9853cce2014-11-17 14:22:48 -0800761
cdalton93a379b2016-05-11 13:58:08 -0700762 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000763 if (Element::kRect_Type == element->getType()) {
cdalton862cff32016-05-12 15:09:48 -0700764 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE,
765 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700767 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700768 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700769 args.fResourceProvider = this->getContext()->resourceProvider();
770 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700771 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700772 args.fColor = GrColor_WHITE;
773 args.fViewMatrix = &viewMatrix;
774 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700775 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700776 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700777 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700778 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 }
780 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000781 // The view matrix is setup to do clip space -> stencil space translation, so
782 // draw rect in clip space.
cdalton862cff32016-05-12 15:09:48 -0700783 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix,
joshualitta8b84992016-01-13 13:35:35 -0800784 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 }
786 }
787 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 return true;
790}
791
bsalomon@google.com411dad02012-06-05 20:24:20 +0000792////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700793sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
794 GrContext* context,
795 int32_t elementsGenID,
796 GrReducedClip::InitialState initialState,
797 const GrReducedClip::ElementList& elements,
798 const SkVector& clipToMaskOffset,
799 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700800 GrUniqueKey key;
801 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips391395d2016-03-02 09:26:36 -0800802 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700803 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700804 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000805 }
806
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000807 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
808 // the top left corner of the resulting rect to the top left of the texture.
809 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
810
robertphillips391395d2016-03-02 09:26:36 -0800811 GrSWMaskHelper helper(context);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000812
joshualitt8059eb92014-12-29 15:10:07 -0800813 // Set the matrix so that rendered clip elements are transformed to mask space from clip
814 // space.
815 SkMatrix translate;
816 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800817
robertphillips98377402016-05-13 05:47:23 -0700818 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700819 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000820
tfarinabf54e492014-10-23 17:47:18 -0700821 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000822 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000823 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000824
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000825 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
826 // Intersect and reverse difference require modifying pixels outside of the geometry
827 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
828 // but leave the pixels inside the geometry alone. For reverse difference we invert all
829 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000830 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000831 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000832 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700833 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000834 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000835 SkPath clipPath;
836 element->asPath(&clipPath);
837 clipPath.toggleInverseFillType();
robertphillips98377402016-05-13 05:47:23 -0700838 helper.drawPath(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op,
839 element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000840 continue;
841 }
842
843 // The other ops (union, xor, diff) only affect pixels inside
844 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000845 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700846 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000847 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000848 SkPath path;
849 element->asPath(&path);
robertphillips98377402016-05-13 05:47:23 -0700850 helper.drawPath(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000851 }
852 }
853
krajcevskiad1dc582014-06-10 15:06:47 -0700854 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800855 GrSurfaceDesc desc;
856 desc.fWidth = clipSpaceIBounds.width();
857 desc.fHeight = clipSpaceIBounds.height();
858 desc.fConfig = kAlpha_8_GrPixelConfig;
859
robertphillipsc99b8f02016-05-15 07:53:35 -0700860 sk_sp<GrTexture> result(context->resourceProvider()->createApproxTexture(desc, 0));
robertphillips391395d2016-03-02 09:26:36 -0800861 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700862 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700863 }
robertphillips391395d2016-03-02 09:26:36 -0800864 result->resourcePriv().setUniqueKey(key);
865
robertphillipsc99b8f02016-05-15 07:53:35 -0700866 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000867
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000868 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000869}