blob: 0d2d5b073e7c1cbdd2f2e56019b216c625883e19 [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
bsalomon0ba8c242015-10-07 09:20:28 -070034static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000035 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080036 // We use device coords to compute the texture coordinates. We set our matrix to be a
37 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000040 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000042 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070043 return GrTextureDomainEffect::Create(result,
44 mat,
45 GrTextureDomain::MakeTexelDomain(result, domainTexels),
46 GrTextureDomain::kDecal_Mode,
47 GrTextureParams::kNone_FilterMode,
48 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049}
50
joshualitta8b84992016-01-13 13:35:35 -080051static void draw_non_aa_rect(GrDrawTarget* drawTarget,
52 const GrPipelineBuilder& pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -070053 const GrClip& clip,
joshualitta8b84992016-01-13 13:35:35 -080054 GrColor color,
55 const SkMatrix& viewMatrix,
56 const SkRect& rect) {
57 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
58 nullptr, nullptr));
cdalton862cff32016-05-12 15:09:48 -070059 drawTarget->drawBatch(pipelineBuilder, clip, batch);
joshualitta8b84992016-01-13 13:35:35 -080060}
61
robertphillips3f7357f2015-10-27 07:17:33 -070062// Does the path in 'element' require SW rendering? If so, return true (and,
63// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
64// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070065bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070066 bool hasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -070067 const GrRenderTarget* rt,
68 const SkMatrix& viewMatrix,
69 const Element* element,
70 GrPathRenderer** prOut,
71 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070072 if (Element::kRect_Type == element->getType()) {
73 // rects can always be drawn directly w/o using the software path
74 // TODO: skip rrects once we're drawing them directly.
75 if (prOut) {
76 *prOut = nullptr;
77 }
78 return false;
79 } else {
80 // We shouldn't get here with an empty clip element.
81 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070082
robertphillips3f7357f2015-10-27 07:17:33 -070083 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
84 SkPath path;
85 element->asPath(&path);
86 if (path.isInverseFillType()) {
87 path.toggleInverseFillType();
88 }
halcanary9d524f22016-03-29 09:03:52 -070089
robertphillips3f7357f2015-10-27 07:17:33 -070090 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070091
robertphillips423e3372015-10-27 09:23:38 -070092 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070093 type = element->isAA()
94 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
95 : GrPathRendererChain::kStencilAndColor_DrawType;
96 } else {
97 type = element->isAA()
98 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070099 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700100 }
halcanary9d524f22016-03-29 09:03:52 -0700101
robertphillips68737822015-10-29 12:12:21 -0700102 GrPathRenderer::CanDrawPathArgs canDrawArgs;
103 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
104 canDrawArgs.fViewMatrix = &viewMatrix;
105 canDrawArgs.fPath = &path;
bsalomon6663acf2016-05-10 09:14:17 -0700106 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700107 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700108 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700109 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
110
robertphillips3f7357f2015-10-27 07:17:33 -0700111 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700112 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700113 if (prOut) {
114 *prOut = pr;
115 }
116 return SkToBool(!pr);
117 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000118}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000119
robertphillips423e3372015-10-27 09:23:38 -0700120// Determines whether it is possible to draw the element to both the stencil buffer and the
121// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
122// also returned.
robertphillips68737822015-10-29 12:12:21 -0700123GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context,
124 GrTexture* texture,
125 const SkMatrix& viewMatrix,
126 const SkClipStack::Element* element) {
robertphillips544b9aa2015-10-28 11:01:41 -0700127 GrPathRenderer* pr;
cdalton93a379b2016-05-11 13:58:08 -0700128 constexpr bool kNeedsStencil = true;
129 constexpr bool kHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -0700130 PathNeedsSWRenderer(context,
cdalton93a379b2016-05-11 13:58:08 -0700131 kHasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -0700132 texture->asRenderTarget(),
133 viewMatrix,
134 element,
135 &pr,
136 kNeedsStencil);
robertphillips544b9aa2015-10-28 11:01:41 -0700137 return pr;
robertphillips423e3372015-10-27 09:23:38 -0700138}
139
robertphillips544b9aa2015-10-28 11:01:41 -0700140GrContext* GrClipMaskManager::getContext() {
141 return fDrawTarget->cmmAccess().context();
142}
bsalomonedd77a12015-05-29 09:45:57 -0700143
robertphillips544b9aa2015-10-28 11:01:41 -0700144const GrCaps* GrClipMaskManager::caps() const {
145 return fDrawTarget->caps();
146}
147
148GrResourceProvider* GrClipMaskManager::resourceProvider() {
149 return fDrawTarget->cmmAccess().resourceProvider();
150}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151/*
152 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
153 * will be used on any element. If so, it returns true to indicate that the
154 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
155 */
robertphillips391395d2016-03-02 09:26:36 -0800156bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
157 const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700158 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800159 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800160 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000161 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000162 // a clip gets complex enough it can just be done in SW regardless
163 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000164
joshualitt8059eb92014-12-29 15:10:07 -0800165 // Set the matrix so that rendered clip elements are transformed to mask space from clip
166 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700167 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800168
tfarinabf54e492014-10-23 17:47:18 -0700169 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000170 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700171
172 SkRegion::Op op = element->getOp();
173 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700174 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700175 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700176
cdalton93a379b2016-05-11 13:58:08 -0700177 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips68737822015-10-29 12:12:21 -0700178 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700179 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000180 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000181 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000182 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000183}
184
bsalomon0b5b6b22015-10-14 08:31:34 -0700185bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700186 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700187 SkVector& clipToRTOffset,
188 const SkRect* drawBounds,
189 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700191 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 boundsInClipSpace = *drawBounds;
193 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
194 }
bsalomon0ba8c242015-10-07 09:20:28 -0700195 SkASSERT(elements.count() <= kMaxAnalyticElements);
196 const GrFragmentProcessor* fps[kMaxAnalyticElements];
197 for (int i = 0; i < kMaxAnalyticElements; ++i) {
198 fps[i] = nullptr;
199 }
200 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700201 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700203 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 SkRegion::Op op = iter.get()->getOp();
205 bool invert;
206 bool skip = false;
207 switch (op) {
208 case SkRegion::kReplace_Op:
209 SkASSERT(iter.get() == elements.head());
210 // Fallthrough, handled same as intersect.
211 case SkRegion::kIntersect_Op:
212 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700213 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 skip = true;
215 }
216 break;
217 case SkRegion::kDifference_Op:
218 invert = true;
219 // We don't currently have a cheap test for whether a rect is fully outside an
220 // element's primitive, so don't attempt to set skip.
221 break;
222 default:
223 failed = true;
224 break;
225 }
226 if (failed) {
227 break;
228 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000229 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700230 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800231 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700232 if (abortIfAA) {
233 failed = true;
234 break;
235 }
joshualittb0a8a372014-09-23 09:50:21 -0700236 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700237 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700239 edgeType =
240 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 }
bsalomona912dde2015-10-14 15:01:50 -0700242
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 switch (iter.get()->getType()) {
244 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700245 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
246 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000247 break;
248 case SkClipStack::Element::kRRect_Type: {
249 SkRRect rrect = iter.get()->getRRect();
250 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700251 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000252 break;
253 }
254 case SkClipStack::Element::kRect_Type: {
255 SkRect rect = iter.get()->getRect();
256 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700257 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000258 break;
259 }
260 default:
261 break;
262 }
bsalomon0ba8c242015-10-07 09:20:28 -0700263 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 failed = true;
265 break;
266 }
bsalomon0ba8c242015-10-07 09:20:28 -0700267 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000268 }
mtklein217daa72014-07-02 12:55:21 -0700269 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 }
271
bsalomon0b5b6b22015-10-14 08:31:34 -0700272 *resultFP = nullptr;
273 if (!failed && fpCnt) {
274 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 }
bsalomon0ba8c242015-10-07 09:20:28 -0700276 for (int i = 0; i < fpCnt; ++i) {
277 fps[i]->unref();
278 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700279 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000280}
281
robertphillips@google.comf294b772012-04-27 14:29:26 +0000282////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000283// sort out what kind of clip mask needs to be created: alpha, stencil,
284// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700285bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
cdalton846c0512016-05-13 10:25:00 -0700286 const GrClipStackClip& clip,
bsalomon0ba8c242015-10-07 09:20:28 -0700287 const SkRect* devBounds,
288 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700289 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
290 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700291 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000292
bsalomonf045d602015-11-18 19:01:12 -0800293 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800294 int32_t genID = 0;
295 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000296 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800297 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700298 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000299
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000300 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700301 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302
joshualitt44701df2015-02-23 14:44:57 -0800303 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
cdalton846c0512016-05-13 10:25:00 -0700304 clipSpaceRTIBounds.offset(clip.origin());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305
cdalton846c0512016-05-13 10:25:00 -0700306 SkIRect clipSpaceReduceQueryBounds;
307#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
308 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
309 SkIRect devIBounds = devBounds->roundOut();
310 devIBounds.offset(clip.origin());
311 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800312 return false;
313 }
cdalton846c0512016-05-13 10:25:00 -0700314 } else {
315 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
bsalomon96e02a82015-03-06 07:13:01 -0800316 }
cdalton846c0512016-05-13 10:25:00 -0700317 GrReducedClip::ReduceClipStack(*clip.clipStack(),
318 clipSpaceReduceQueryBounds,
319 &elements,
320 &genID,
321 &initialState,
322 &clipSpaceIBounds,
323 &requiresAA);
324 if (elements.isEmpty()) {
325 if (GrReducedClip::kAllIn_InitialState == initialState) {
326 if (clipSpaceIBounds == clipSpaceRTIBounds) {
327 return true;
328 }
329 } else {
330 return false;
331 }
332 }
cdalton93a379b2016-05-11 13:58:08 -0700333
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000334 // An element count of 4 was chosen because of the common pattern in Blink of:
335 // isect RR
336 // diff RR
337 // isect convex_poly
338 // isect convex_poly
339 // when drawing rounded div borders. This could probably be tuned based on a
340 // configuration's relative costs of switching RTs to generate a mask vs
341 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700342 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800343 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
344 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800345 // When there are multiple samples we want to do per-sample clipping, not compute a
346 // fractional pixel coverage.
cdalton3ccf2e72016-05-06 09:41:16 -0700347 bool disallowAnalyticAA = rt->isStencilBufferMultisampled();
348 if (disallowAnalyticAA && !rt->numColorSamples()) {
349 // With a single color sample, any coverage info is lost from color once it hits the
350 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
351 // is multisampled.
352 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700353 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700354 }
bsalomon0ba8c242015-10-07 09:20:28 -0700355 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000356 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700357 (requiresAA &&
358 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
359 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700360 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800361 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700362 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700363 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700364 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000365 }
bsalomon0ba8c242015-10-07 09:20:28 -0700366 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000367 return true;
368 }
369 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000370
cdaltonede75742015-11-11 15:27:57 -0800371 // If the stencil buffer is multisampled we can use it to do everything.
372 if (!rt->isStencilBufferMultisampled() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700373 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374
joshualitt8059eb92014-12-29 15:10:07 -0800375 // The top-left of the mask corresponds to the top-left corner of the bounds.
376 SkVector clipToMaskOffset = {
377 SkIntToScalar(-clipSpaceIBounds.fLeft),
378 SkIntToScalar(-clipSpaceIBounds.fTop)
379 };
380
robertphillips391395d2016-03-02 09:26:36 -0800381 if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000382 // The clip geometry is complex enough that it will be more efficient to create it
383 // entirely in software
robertphillips391395d2016-03-02 09:26:36 -0800384 result.reset(CreateSoftwareClipMask(this->getContext(),
385 genID,
386 initialState,
387 elements,
388 clipToMaskOffset,
389 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000390 } else {
robertphillips391395d2016-03-02 09:26:36 -0800391 result.reset(CreateAlphaClipMask(this->getContext(),
392 genID,
393 initialState,
394 elements,
395 clipToMaskOffset,
396 clipSpaceIBounds));
397 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700398 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000399 }
400
bsalomon49f085d2014-09-05 13:34:00 -0700401 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 // The mask's top left coord should be pinned to the rounded-out top left corner of
403 // clipSpace bounds. We determine the mask's position WRT to the render target here.
404 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800405 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700406 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000407 return true;
408 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000409 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000412 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800413 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800414 this->createStencilClipMask(rt,
415 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000416 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417 elements,
418 clipSpaceIBounds,
419 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000420
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
422 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
423 // use both stencil and scissor test to the bounds for the final draw.
424 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
425 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700426 out->fScissorState.set(scissorSpaceIBounds);
cdalton93a379b2016-05-11 13:58:08 -0700427 out->fHasStencilClip = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000428 return true;
429}
430
robertphillips391395d2016-03-02 09:26:36 -0800431static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700432 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700433 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800434 const SkMatrix& viewMatrix,
435 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800436
437 // TODO: Draw rrects directly here.
438 switch (element->getType()) {
439 case Element::kEmpty_Type:
440 SkDEBUGFAIL("Should never get here with an empty element.");
441 break;
robertphillips391395d2016-03-02 09:26:36 -0800442 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700443 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800444 element->getOp(),
445 element->isInverseFilled(),
446 element->isAA(),
447 viewMatrix, element->getRect());
448 break;
robertphillips86c60752016-03-02 08:43:13 -0800449 default: {
450 SkPath path;
451 element->asPath(&path);
452 if (path.isInverseFillType()) {
453 path.toggleInverseFillType();
454 }
455
cdalton862cff32016-05-12 15:09:48 -0700456 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800457 element->getOp(),
458 element->isInverseFilled(),
459 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800460 break;
461 }
462 }
robertphillips391395d2016-03-02 09:26:36 -0800463
464 return false;
465}
466
467static void draw_element(GrDrawContext* dc,
468 const GrClip& clip, // TODO: can this just always be WideOpen?
469 const GrPaint &paint,
470 const SkMatrix& viewMatrix,
471 const SkClipStack::Element* element) {
472
473 // TODO: Draw rrects directly here.
474 switch (element->getType()) {
475 case Element::kEmpty_Type:
476 SkDEBUGFAIL("Should never get here with an empty element.");
477 break;
478 case Element::kRect_Type:
479 dc->drawRect(clip, paint, viewMatrix, element->getRect());
480 break;
481 default: {
482 SkPath path;
483 element->asPath(&path);
484 if (path.isInverseFillType()) {
485 path.toggleInverseFillType();
486 }
487
bsalomon6663acf2016-05-10 09:14:17 -0700488 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800489 break;
490 }
491 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492}
493
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700495// Create a 8-bit clip mask in alpha
496
497static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
498 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
499 GrUniqueKey::Builder builder(key, kDomain, 3);
500 builder[0] = clipGenID;
501 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
502 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
503}
504
robertphillips391395d2016-03-02 09:26:36 -0800505GrTexture* GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
506 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700507 GrReducedClip::InitialState initialState,
508 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800509 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800511 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700512 GrUniqueKey key;
513 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
514 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700515 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000516 }
517
robertphillips544b9aa2015-10-28 11:01:41 -0700518 // There's no texture in the cache. Let's try to allocate it then.
robertphillips391395d2016-03-02 09:26:36 -0800519 GrSurfaceDesc desc;
520 desc.fWidth = clipSpaceIBounds.width();
521 desc.fHeight = clipSpaceIBounds.height();
522 desc.fFlags = kRenderTarget_GrSurfaceFlag;
523 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
524 desc.fConfig = kAlpha_8_GrPixelConfig;
525 } else {
526 desc.fConfig = kRGBA_8888_GrPixelConfig;
527 }
528
529 SkAutoTUnref<GrTexture> texture(resourceProvider->createApproxTexture(desc, 0));
bsalomon473addf2015-10-02 07:49:05 -0700530 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700531 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 }
533
robertphillips391395d2016-03-02 09:26:36 -0800534 texture->resourcePriv().setUniqueKey(key);
535
robertphillips6c7e3252016-04-27 10:47:51 -0700536 sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(texture->asRenderTarget())));
robertphillips391395d2016-03-02 09:26:36 -0800537 if (!dc) {
538 return nullptr;
539 }
joshualitt8059eb92014-12-29 15:10:07 -0800540
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 // The texture may be larger than necessary, this rect represents the part of the texture
542 // we populate with a rasterization of the clip.
543 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
544
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000545 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
546 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800547 dc->clear(&maskSpaceIBounds,
548 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
549 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000550
robertphillips391395d2016-03-02 09:26:36 -0800551 // Set the matrix so that rendered clip elements are transformed to mask space from clip
552 // space.
553 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
554
555 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
557 // pass must not set values outside of this bounds or stencil values outside the rect won't be
558 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800559
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700561 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000563 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000565 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips391395d2016-03-02 09:26:36 -0800566#ifdef SK_DEBUG
567 GrPathRenderer* pr = GetPathRenderer(context,
robertphillips68737822015-10-29 12:12:21 -0700568 texture, translate, element);
robertphillips544b9aa2015-10-28 11:01:41 -0700569 if (Element::kRect_Type != element->getType() && !pr) {
robertphillips391395d2016-03-02 09:26:36 -0800570 // UseSWOnlyPath should now filter out all cases where gpu-side mask merging would
571 // be performed (i.e., pr would be NULL for a non-rect path).
572 // See https://bug.skia.org/4519 for rationale and details.
robertphillips544b9aa2015-10-28 11:01:41 -0700573 SkASSERT(0);
robertphillips391395d2016-03-02 09:26:36 -0800574 }
575#endif
576
cdalton846c0512016-05-13 10:25:00 -0700577 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700578
robertphillips391395d2016-03-02 09:26:36 -0800579 // draw directly into the result with the stencil set to make the pixels affected
580 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700581 static constexpr GrUserStencilSettings kStencilInElement(
582 GrUserStencilSettings::StaticInit<
583 0xffff,
584 GrUserStencilTest::kAlways,
585 0xffff,
586 GrUserStencilOp::kReplace,
587 GrUserStencilOp::kReplace,
588 0xffff>()
589 );
cdalton862cff32016-05-12 15:09:48 -0700590 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800591 translate, element)) {
592 texture->resourcePriv().removeUniqueKey();
593 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000594 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000595
robertphillips391395d2016-03-02 09:26:36 -0800596 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700597 static constexpr GrUserStencilSettings kDrawOutsideElement(
598 GrUserStencilSettings::StaticInit<
599 0x0000,
600 GrUserStencilTest::kEqual,
601 0xffff,
602 GrUserStencilOp::kZero,
603 GrUserStencilOp::kZero,
604 0xffff>()
605 );
cdalton862cff32016-05-12 15:09:48 -0700606 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800607 op, !invert, false,
608 translate,
609 SkRect::Make(clipSpaceIBounds))) {
610 texture->resourcePriv().removeUniqueKey();
611 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000612 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800614 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800615 GrPaint paint;
616 paint.setAntiAlias(element->isAA());
617 paint.setCoverageSetOpXPFactory(op, false);
618
cdalton846c0512016-05-13 10:25:00 -0700619 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620 }
621 }
622
mtklein18300a32016-03-16 13:53:35 -0700623 return texture.release();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624}
625
626////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000627// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000628// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800629bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
630 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700631 GrReducedClip::InitialState initialState,
632 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000633 const SkIRect& clipSpaceIBounds,
634 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700635 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636
robertphillips544b9aa2015-10-28 11:01:41 -0700637 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700638 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 return false;
640 }
641
egdaniel8dc7c3a2015-04-16 11:22:42 -0700642 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
643 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000644 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
645 SkVector translate = {
646 SkIntToScalar(clipSpaceToStencilOffset.fX),
647 SkIntToScalar(clipSpaceToStencilOffset.fY)
648 };
joshualitt8059eb92014-12-29 15:10:07 -0800649 SkMatrix viewMatrix;
650 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000651
bsalomon@google.com9f131742012-12-13 20:43:56 +0000652 // We set the current clip to the bounds so that our recursive draws are scissored to them.
653 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
654 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700655 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656
bsalomonb3b9aec2015-09-10 11:16:35 -0700657 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
658 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659
660 // walk through each clip element and perform its set op
661 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700662 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000663 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800664
egdaniel8dd688b2015-01-22 10:16:09 -0800665 GrPipelineBuilder pipelineBuilder;
666 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800667
egdaniel8dd688b2015-01-22 10:16:09 -0800668 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800669
670 // if the target is MSAA then we want MSAA enabled when the clip is soft
cdaltonede75742015-11-11 15:27:57 -0800671 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700672 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800673 }
674
tomhudson@google.com8afae612012-08-14 15:03:35 +0000675 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700677 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000679 // This will be used to determine whether the clip shape can be rendered into the
680 // stencil with arbitrary stencil settings.
681 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000683 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684
halcanary96fcdcc2015-08-27 07:41:13 -0700685 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000686 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000687 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000688 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000690 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000691 element->asPath(&clipPath);
692 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000693 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000694 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000695 }
robertphillips68737822015-10-29 12:12:21 -0700696
cdalton93a379b2016-05-11 13:58:08 -0700697 SkASSERT(!pipelineBuilder.hasUserStencilSettings());
robertphillips68737822015-10-29 12:12:21 -0700698
699 GrPathRenderer::CanDrawPathArgs canDrawArgs;
700 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
701 canDrawArgs.fViewMatrix = &viewMatrix;
702 canDrawArgs.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700703 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700704 canDrawArgs.fAntiAlias = false;
cdalton93a379b2016-05-11 13:58:08 -0700705 canDrawArgs.fHasUserStencilSettings = pipelineBuilder.hasUserStencilSettings();
robertphillips68737822015-10-29 12:12:21 -0700706 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
707
708 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
709 GrPathRendererChain::kStencilOnly_DrawType,
710 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700711 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 return false;
713 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 }
715
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000716 bool canRenderDirectToStencil =
717 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700718 bool drawDirectToClip; // Given the renderer, the element,
719 // fill rule, and set operation should
720 // we render the element directly to
721 // stencil bit used for clipping.
722 GrUserStencilSettings const* const* stencilPasses =
723 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
724 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725
726 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700727 if (!drawDirectToClip) {
728 static constexpr GrUserStencilSettings kDrawToStencil(
729 GrUserStencilSettings::StaticInit<
730 0x0000,
731 GrUserStencilTest::kAlways,
732 0xffff,
733 GrUserStencilOp::kIncMaybeClamp,
734 GrUserStencilOp::kIncMaybeClamp,
735 0xffff>()
736 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000737 if (Element::kRect_Type == element->getType()) {
cdalton93a379b2016-05-11 13:58:08 -0700738 pipelineBuilder.setUserStencil(&kDrawToStencil);
joshualitt73bb4562015-03-25 07:16:21 -0700739
cdalton862cff32016-05-12 15:09:48 -0700740 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix,
joshualitta8b84992016-01-13 13:35:35 -0800741 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000743 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000744 if (canRenderDirectToStencil) {
cdalton93a379b2016-05-11 13:58:08 -0700745 pipelineBuilder.setUserStencil(&kDrawToStencil);
bsalomon0aff2fa2015-07-31 06:48:27 -0700746
747 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700748 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700749 args.fResourceProvider = this->getContext()->resourceProvider();
750 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700751 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700752 args.fColor = GrColor_WHITE;
753 args.fViewMatrix = &viewMatrix;
754 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700755 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700756 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700757 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700758 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000759 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700760 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700761 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700762 args.fResourceProvider = this->getContext()->resourceProvider();
763 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700764 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700765 args.fViewMatrix = &viewMatrix;
766 args.fPath = &clipPath;
bsalomon0aff2fa2015-07-31 06:48:27 -0700767 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000768 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769 }
770 }
771 }
772
773 // now we modify the clip bit by rendering either the clip
774 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700775 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700776 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
777 pipelineBuilder.setUserStencil(*pass);
joshualitt9853cce2014-11-17 14:22:48 -0800778
cdalton93a379b2016-05-11 13:58:08 -0700779 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000780 if (Element::kRect_Type == element->getType()) {
cdalton862cff32016-05-12 15:09:48 -0700781 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE,
782 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700784 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700785 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700786 args.fResourceProvider = this->getContext()->resourceProvider();
787 args.fPipelineBuilder = &pipelineBuilder;
cdalton862cff32016-05-12 15:09:48 -0700788 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700789 args.fColor = GrColor_WHITE;
790 args.fViewMatrix = &viewMatrix;
791 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700792 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700793 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700794 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700795 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 }
797 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000798 // The view matrix is setup to do clip space -> stencil space translation, so
799 // draw rect in clip space.
cdalton862cff32016-05-12 15:09:48 -0700800 draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix,
joshualitta8b84992016-01-13 13:35:35 -0800801 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 }
803 }
804 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 return true;
807}
808
bsalomon@google.com411dad02012-06-05 20:24:20 +0000809////////////////////////////////////////////////////////////////////////////////
robertphillips391395d2016-03-02 09:26:36 -0800810GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context,
811 int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000812 GrReducedClip::InitialState initialState,
813 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800814 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000815 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700816 GrUniqueKey key;
817 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips391395d2016-03-02 09:26:36 -0800818 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700819 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
820 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000821 }
822
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000823 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
824 // the top left corner of the resulting rect to the top left of the texture.
825 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
826
robertphillips391395d2016-03-02 09:26:36 -0800827 GrSWMaskHelper helper(context);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000828
joshualitt8059eb92014-12-29 15:10:07 -0800829 // Set the matrix so that rendered clip elements are transformed to mask space from clip
830 // space.
831 SkMatrix translate;
832 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800833
robertphillips98377402016-05-13 05:47:23 -0700834 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700835 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000836
tfarinabf54e492014-10-23 17:47:18 -0700837 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000838 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000839 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000840
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000841 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
842 // Intersect and reverse difference require modifying pixels outside of the geometry
843 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
844 // but leave the pixels inside the geometry alone. For reverse difference we invert all
845 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000846 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000847 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000848 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700849 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000850 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000851 SkPath clipPath;
852 element->asPath(&clipPath);
853 clipPath.toggleInverseFillType();
robertphillips98377402016-05-13 05:47:23 -0700854 helper.drawPath(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op,
855 element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000856 continue;
857 }
858
859 // The other ops (union, xor, diff) only affect pixels inside
860 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000861 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700862 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000863 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000864 SkPath path;
865 element->asPath(&path);
robertphillips98377402016-05-13 05:47:23 -0700866 helper.drawPath(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000867 }
868 }
869
krajcevskiad1dc582014-06-10 15:06:47 -0700870 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800871 GrSurfaceDesc desc;
872 desc.fWidth = clipSpaceIBounds.width();
873 desc.fHeight = clipSpaceIBounds.height();
874 desc.fConfig = kAlpha_8_GrPixelConfig;
875
876 GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0);
877 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700878 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700879 }
robertphillips391395d2016-03-02 09:26:36 -0800880 result->resourcePriv().setUniqueKey(key);
881
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +0000882 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000883
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000884 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000885}