blob: c7b88caa244cef3abf9ae59abab5097f919af3a3 [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,
53 GrColor color,
54 const SkMatrix& viewMatrix,
55 const SkRect& rect) {
56 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
57 nullptr, nullptr));
58 drawTarget->drawBatch(pipelineBuilder, batch);
59}
60
robertphillips3f7357f2015-10-27 07:17:33 -070061// Does the path in 'element' require SW rendering? If so, return true (and,
62// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
63// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070064bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070065 bool hasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -070066 const GrRenderTarget* rt,
67 const SkMatrix& viewMatrix,
68 const Element* element,
69 GrPathRenderer** prOut,
70 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070071 if (Element::kRect_Type == element->getType()) {
72 // rects can always be drawn directly w/o using the software path
73 // TODO: skip rrects once we're drawing them directly.
74 if (prOut) {
75 *prOut = nullptr;
76 }
77 return false;
78 } else {
79 // We shouldn't get here with an empty clip element.
80 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070081
robertphillips3f7357f2015-10-27 07:17:33 -070082 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
83 SkPath path;
84 element->asPath(&path);
85 if (path.isInverseFillType()) {
86 path.toggleInverseFillType();
87 }
halcanary9d524f22016-03-29 09:03:52 -070088
robertphillips3f7357f2015-10-27 07:17:33 -070089 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070090
robertphillips423e3372015-10-27 09:23:38 -070091 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070092 type = element->isAA()
93 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
94 : GrPathRendererChain::kStencilAndColor_DrawType;
95 } else {
96 type = element->isAA()
97 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070098 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -070099 }
halcanary9d524f22016-03-29 09:03:52 -0700100
robertphillips68737822015-10-29 12:12:21 -0700101 GrPathRenderer::CanDrawPathArgs canDrawArgs;
102 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
103 canDrawArgs.fViewMatrix = &viewMatrix;
104 canDrawArgs.fPath = &path;
bsalomon6663acf2016-05-10 09:14:17 -0700105 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700106 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700107 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700108 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
109
robertphillips3f7357f2015-10-27 07:17:33 -0700110 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700111 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700112 if (prOut) {
113 *prOut = pr;
114 }
115 return SkToBool(!pr);
116 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000117}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000118
robertphillips423e3372015-10-27 09:23:38 -0700119// Determines whether it is possible to draw the element to both the stencil buffer and the
120// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
121// also returned.
robertphillips68737822015-10-29 12:12:21 -0700122GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context,
123 GrTexture* texture,
124 const SkMatrix& viewMatrix,
125 const SkClipStack::Element* element) {
robertphillips544b9aa2015-10-28 11:01:41 -0700126 GrPathRenderer* pr;
cdalton93a379b2016-05-11 13:58:08 -0700127 constexpr bool kNeedsStencil = true;
128 constexpr bool kHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -0700129 PathNeedsSWRenderer(context,
cdalton93a379b2016-05-11 13:58:08 -0700130 kHasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -0700131 texture->asRenderTarget(),
132 viewMatrix,
133 element,
134 &pr,
135 kNeedsStencil);
robertphillips544b9aa2015-10-28 11:01:41 -0700136 return pr;
robertphillips423e3372015-10-27 09:23:38 -0700137}
138
bsalomon69cfe952015-11-30 13:27:47 -0800139GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget, bool debugClipBatchToBounds)
bsalomonc988d2c2015-10-07 09:30:05 -0700140 : fDrawTarget(drawTarget)
bsalomon69cfe952015-11-30 13:27:47 -0800141 , fClipMode(kIgnoreClip_StencilClipMode)
142 , fDebugClipBatchToBounds(debugClipBatchToBounds) {
bsalomonedd77a12015-05-29 09:45:57 -0700143}
144
robertphillips544b9aa2015-10-28 11:01:41 -0700145GrContext* GrClipMaskManager::getContext() {
146 return fDrawTarget->cmmAccess().context();
147}
bsalomonedd77a12015-05-29 09:45:57 -0700148
robertphillips544b9aa2015-10-28 11:01:41 -0700149const GrCaps* GrClipMaskManager::caps() const {
150 return fDrawTarget->caps();
151}
152
153GrResourceProvider* GrClipMaskManager::resourceProvider() {
154 return fDrawTarget->cmmAccess().resourceProvider();
155}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000156/*
157 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
158 * will be used on any element. If so, it returns true to indicate that the
159 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
160 */
robertphillips391395d2016-03-02 09:26:36 -0800161bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
162 const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700163 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800164 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800165 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000166 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000167 // a clip gets complex enough it can just be done in SW regardless
168 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000169
joshualitt8059eb92014-12-29 15:10:07 -0800170 // Set the matrix so that rendered clip elements are transformed to mask space from clip
171 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700172 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800173
tfarinabf54e492014-10-23 17:47:18 -0700174 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000175 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700176
177 SkRegion::Op op = element->getOp();
178 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700179 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700180 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700181
cdalton93a379b2016-05-11 13:58:08 -0700182 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips68737822015-10-29 12:12:21 -0700183 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700184 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000185 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000186 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000188}
189
bsalomon0b5b6b22015-10-14 08:31:34 -0700190bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700191 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700192 SkVector& clipToRTOffset,
193 const SkRect* drawBounds,
194 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700196 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 boundsInClipSpace = *drawBounds;
198 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
199 }
bsalomon0ba8c242015-10-07 09:20:28 -0700200 SkASSERT(elements.count() <= kMaxAnalyticElements);
201 const GrFragmentProcessor* fps[kMaxAnalyticElements];
202 for (int i = 0; i < kMaxAnalyticElements; ++i) {
203 fps[i] = nullptr;
204 }
205 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700206 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700208 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 SkRegion::Op op = iter.get()->getOp();
210 bool invert;
211 bool skip = false;
212 switch (op) {
213 case SkRegion::kReplace_Op:
214 SkASSERT(iter.get() == elements.head());
215 // Fallthrough, handled same as intersect.
216 case SkRegion::kIntersect_Op:
217 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700218 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 skip = true;
220 }
221 break;
222 case SkRegion::kDifference_Op:
223 invert = true;
224 // We don't currently have a cheap test for whether a rect is fully outside an
225 // element's primitive, so don't attempt to set skip.
226 break;
227 default:
228 failed = true;
229 break;
230 }
231 if (failed) {
232 break;
233 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700235 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800236 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700237 if (abortIfAA) {
238 failed = true;
239 break;
240 }
joshualittb0a8a372014-09-23 09:50:21 -0700241 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700242 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700244 edgeType =
245 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 }
bsalomona912dde2015-10-14 15:01:50 -0700247
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 switch (iter.get()->getType()) {
249 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700250 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
251 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000252 break;
253 case SkClipStack::Element::kRRect_Type: {
254 SkRRect rrect = iter.get()->getRRect();
255 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700256 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 break;
258 }
259 case SkClipStack::Element::kRect_Type: {
260 SkRect rect = iter.get()->getRect();
261 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700262 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263 break;
264 }
265 default:
266 break;
267 }
bsalomon0ba8c242015-10-07 09:20:28 -0700268 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000269 failed = true;
270 break;
271 }
bsalomon0ba8c242015-10-07 09:20:28 -0700272 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000273 }
mtklein217daa72014-07-02 12:55:21 -0700274 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 }
276
bsalomon0b5b6b22015-10-14 08:31:34 -0700277 *resultFP = nullptr;
278 if (!failed && fpCnt) {
279 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000280 }
bsalomon0ba8c242015-10-07 09:20:28 -0700281 for (int i = 0; i < fpCnt; ++i) {
282 fps[i]->unref();
283 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700284 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000285}
286
bsalomon69cfe952015-11-30 13:27:47 -0800287static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) {
288 switch (clip.clipType()) {
289 case GrClip::kClipStack_ClipType: {
290 SkClipStack* stack = new SkClipStack;
291 *stack = *clip.clipStack();
292 // The stack is actually in clip space not device space.
293 SkRect clipRect = devRect;
294 SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) };
295 clipRect.offset(origin);
296 SkIRect iclipRect;
297 clipRect.roundOut(&iclipRect);
298 clipRect = SkRect::Make(iclipRect);
299 stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false);
300 out->setClipStack(stack, &clip.origin());
301 break;
302 }
303 case GrClip::kWideOpen_ClipType:
304 *out = GrClip(devRect);
305 break;
306 case GrClip::kIRect_ClipType: {
307 SkIRect intersect;
308 devRect.roundOut(&intersect);
309 if (intersect.intersect(clip.irect())) {
310 *out = GrClip(intersect);
311 } else {
312 *out = clip;
313 }
314 break;
315 }
316 }
317}
318
robertphillips391395d2016-03-02 09:26:36 -0800319bool GrClipMaskManager::setupScissorClip(const GrPipelineBuilder& pipelineBuilder,
robertphillips391395d2016-03-02 09:26:36 -0800320 const SkIRect& clipScissor,
321 const SkRect* devBounds,
322 GrAppliedClip* out) {
cdalton93a379b2016-05-11 13:58:08 -0700323 SkASSERT(kModifyClip_StencilClipMode != fClipMode); // TODO: Remove fClipMode.
324 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips391395d2016-03-02 09:26:36 -0800325
326 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
327
328 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
329 SkIRect devBoundsScissor;
330 const SkIRect* scissor = &clipScissor;
331 bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds;
332 if (doDevBoundsClip) {
333 devBounds->roundOut(&devBoundsScissor);
334 if (devBoundsScissor.intersect(clipScissor)) {
335 scissor = &devBoundsScissor;
336 }
337 }
338
339 if (scissor->contains(clipSpaceRTIBounds)) {
340 // This counts as wide open
robertphillips391395d2016-03-02 09:26:36 -0800341 return true;
342 }
343
344 if (clipSpaceRTIBounds.intersect(*scissor)) {
345 out->fScissorState.set(clipSpaceRTIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800346 return true;
347 }
348 return false;
349}
350
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000352// sort out what kind of clip mask needs to be created: alpha, stencil,
353// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700354bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
bsalomon0ba8c242015-10-07 09:20:28 -0700355 const SkRect* devBounds,
356 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700357 if (kRespectClip_StencilClipMode == fClipMode) {
358 fClipMode = kIgnoreClip_StencilClipMode;
359 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000360
bsalomonf045d602015-11-18 19:01:12 -0800361 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800362 int32_t genID = 0;
363 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000364 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800365 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700366 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700369 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000370
joshualitt44701df2015-02-23 14:44:57 -0800371 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon69cfe952015-11-30 13:27:47 -0800372 GrClip devBoundsClip;
373 bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds;
374 if (doDevBoundsClip) {
375 add_rect_to_clip(pipelineBuilder.clip(), *devBounds, &devBoundsClip);
376 }
377 const GrClip& clip = doDevBoundsClip ? devBoundsClip : pipelineBuilder.clip();
378
bsalomon96e02a82015-03-06 07:13:01 -0800379 if (clip.isWideOpen(clipSpaceRTIBounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000380 return true;
381 }
382
bsalomon96e02a82015-03-06 07:13:01 -0800383 // The clip mask manager always draws with a single IRect so we special case that logic here
384 // Image filters just use a rect, so we also special case that logic
385 switch (clip.clipType()) {
386 case GrClip::kWideOpen_ClipType:
387 SkFAIL("Should have caught this with clip.isWideOpen()");
388 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800389 case GrClip::kIRect_ClipType: {
390 SkIRect scissor = clip.irect();
391 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700392 out->fScissorState.set(scissor);
cdalton93a379b2016-05-11 13:58:08 -0700393 out->fHasStencilClip = kIgnoreClip_StencilClipMode != fClipMode;
bsalomon9ce30e12015-03-06 08:42:34 -0800394 return true;
395 }
396 return false;
397 }
bsalomon96e02a82015-03-06 07:13:01 -0800398 case GrClip::kClipStack_ClipType: {
399 clipSpaceRTIBounds.offset(clip.origin());
bsalomondb4758c2015-11-23 11:14:20 -0800400 SkIRect clipSpaceReduceQueryBounds;
bsalomon5b592e82016-03-09 09:55:55 -0800401#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
Brian Salomon362c9002015-11-30 17:02:50 -0500402 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
bsalomondb4758c2015-11-23 11:14:20 -0800403 SkIRect devIBounds = devBounds->roundOut();
404 devIBounds.offset(clip.origin());
405 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
406 return false;
407 }
408 } else {
409 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
410 }
bsalomon96e02a82015-03-06 07:13:01 -0800411 GrReducedClip::ReduceClipStack(*clip.clipStack(),
bsalomondb4758c2015-11-23 11:14:20 -0800412 clipSpaceReduceQueryBounds,
bsalomon96e02a82015-03-06 07:13:01 -0800413 &elements,
414 &genID,
415 &initialState,
416 &clipSpaceIBounds,
417 &requiresAA);
418 if (elements.isEmpty()) {
419 if (GrReducedClip::kAllIn_InitialState == initialState) {
420 if (clipSpaceIBounds == clipSpaceRTIBounds) {
bsalomon96e02a82015-03-06 07:13:01 -0800421 return true;
422 }
423 } else {
424 return false;
425 }
426 }
427 } break;
428 }
429
cdalton93a379b2016-05-11 13:58:08 -0700430 SkASSERT(kIgnoreClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode.
431
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000432 // An element count of 4 was chosen because of the common pattern in Blink of:
433 // isect RR
434 // diff RR
435 // isect convex_poly
436 // isect convex_poly
437 // when drawing rounded div borders. This could probably be tuned based on a
438 // configuration's relative costs of switching RTs to generate a mask vs
439 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700440 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800441 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
442 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800443 // When there are multiple samples we want to do per-sample clipping, not compute a
444 // fractional pixel coverage.
cdalton3ccf2e72016-05-06 09:41:16 -0700445 bool disallowAnalyticAA = rt->isStencilBufferMultisampled();
446 if (disallowAnalyticAA && !rt->numColorSamples()) {
447 // With a single color sample, any coverage info is lost from color once it hits the
448 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
449 // is multisampled.
450 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700451 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700452 }
bsalomon0ba8c242015-10-07 09:20:28 -0700453 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000454 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700455 (requiresAA &&
456 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
457 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700458 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800459 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700460 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700461 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700462 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000463 }
bsalomon0ba8c242015-10-07 09:20:28 -0700464 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000465 return true;
466 }
467 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000468
cdaltonede75742015-11-11 15:27:57 -0800469 // If the stencil buffer is multisampled we can use it to do everything.
470 if (!rt->isStencilBufferMultisampled() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700471 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472
joshualitt8059eb92014-12-29 15:10:07 -0800473 // The top-left of the mask corresponds to the top-left corner of the bounds.
474 SkVector clipToMaskOffset = {
475 SkIntToScalar(-clipSpaceIBounds.fLeft),
476 SkIntToScalar(-clipSpaceIBounds.fTop)
477 };
478
robertphillips391395d2016-03-02 09:26:36 -0800479 if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000480 // The clip geometry is complex enough that it will be more efficient to create it
481 // entirely in software
robertphillips391395d2016-03-02 09:26:36 -0800482 result.reset(CreateSoftwareClipMask(this->getContext(),
483 genID,
484 initialState,
485 elements,
486 clipToMaskOffset,
487 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000488 } else {
robertphillips391395d2016-03-02 09:26:36 -0800489 result.reset(CreateAlphaClipMask(this->getContext(),
490 genID,
491 initialState,
492 elements,
493 clipToMaskOffset,
494 clipSpaceIBounds));
495 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700496 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 }
498
bsalomon49f085d2014-09-05 13:34:00 -0700499 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000500 // The mask's top left coord should be pinned to the rounded-out top left corner of
501 // clipSpace bounds. We determine the mask's position WRT to the render target here.
502 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800503 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700504 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505 return true;
506 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000507 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000508 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000510 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800511 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800512 this->createStencilClipMask(rt,
513 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000514 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 elements,
516 clipSpaceIBounds,
517 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000518
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000519 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
520 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
521 // use both stencil and scissor test to the bounds for the final draw.
522 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
523 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700524 out->fScissorState.set(scissorSpaceIBounds);
cdalton93a379b2016-05-11 13:58:08 -0700525 SkASSERT(kRespectClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode.
526 out->fHasStencilClip = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000527 return true;
528}
529
robertphillips391395d2016-03-02 09:26:36 -0800530static bool stencil_element(GrDrawContext* dc,
531 const SkIRect* scissorRect,
cdalton93a379b2016-05-11 13:58:08 -0700532 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800533 const SkMatrix& viewMatrix,
534 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800535
536 // TODO: Draw rrects directly here.
537 switch (element->getType()) {
538 case Element::kEmpty_Type:
539 SkDEBUGFAIL("Should never get here with an empty element.");
540 break;
robertphillips391395d2016-03-02 09:26:36 -0800541 case Element::kRect_Type:
542 return dc->drawContextPriv().drawAndStencilRect(scissorRect, ss,
543 element->getOp(),
544 element->isInverseFilled(),
545 element->isAA(),
546 viewMatrix, element->getRect());
547 break;
robertphillips86c60752016-03-02 08:43:13 -0800548 default: {
549 SkPath path;
550 element->asPath(&path);
551 if (path.isInverseFillType()) {
552 path.toggleInverseFillType();
553 }
554
robertphillips391395d2016-03-02 09:26:36 -0800555 return dc->drawContextPriv().drawAndStencilPath(scissorRect, ss,
556 element->getOp(),
557 element->isInverseFilled(),
558 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800559 break;
560 }
561 }
robertphillips391395d2016-03-02 09:26:36 -0800562
563 return false;
564}
565
566static void draw_element(GrDrawContext* dc,
567 const GrClip& clip, // TODO: can this just always be WideOpen?
568 const GrPaint &paint,
569 const SkMatrix& viewMatrix,
570 const SkClipStack::Element* element) {
571
572 // TODO: Draw rrects directly here.
573 switch (element->getType()) {
574 case Element::kEmpty_Type:
575 SkDEBUGFAIL("Should never get here with an empty element.");
576 break;
577 case Element::kRect_Type:
578 dc->drawRect(clip, paint, viewMatrix, element->getRect());
579 break;
580 default: {
581 SkPath path;
582 element->asPath(&path);
583 if (path.isInverseFillType()) {
584 path.toggleInverseFillType();
585 }
586
bsalomon6663acf2016-05-10 09:14:17 -0700587 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800588 break;
589 }
590 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591}
592
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000593////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700594// Create a 8-bit clip mask in alpha
595
596static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
597 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
598 GrUniqueKey::Builder builder(key, kDomain, 3);
599 builder[0] = clipGenID;
600 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
601 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
602}
603
robertphillips391395d2016-03-02 09:26:36 -0800604GrTexture* GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
605 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700606 GrReducedClip::InitialState initialState,
607 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800608 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000609 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800610 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700611 GrUniqueKey key;
612 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
613 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700614 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000615 }
616
robertphillips544b9aa2015-10-28 11:01:41 -0700617 // There's no texture in the cache. Let's try to allocate it then.
robertphillips391395d2016-03-02 09:26:36 -0800618 GrSurfaceDesc desc;
619 desc.fWidth = clipSpaceIBounds.width();
620 desc.fHeight = clipSpaceIBounds.height();
621 desc.fFlags = kRenderTarget_GrSurfaceFlag;
622 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
623 desc.fConfig = kAlpha_8_GrPixelConfig;
624 } else {
625 desc.fConfig = kRGBA_8888_GrPixelConfig;
626 }
627
628 SkAutoTUnref<GrTexture> texture(resourceProvider->createApproxTexture(desc, 0));
bsalomon473addf2015-10-02 07:49:05 -0700629 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700630 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631 }
632
robertphillips391395d2016-03-02 09:26:36 -0800633 texture->resourcePriv().setUniqueKey(key);
634
robertphillips6c7e3252016-04-27 10:47:51 -0700635 sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(texture->asRenderTarget())));
robertphillips391395d2016-03-02 09:26:36 -0800636 if (!dc) {
637 return nullptr;
638 }
joshualitt8059eb92014-12-29 15:10:07 -0800639
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 // The texture may be larger than necessary, this rect represents the part of the texture
641 // we populate with a rasterization of the clip.
642 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
643
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000644 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
645 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800646 dc->clear(&maskSpaceIBounds,
647 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
648 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000649
robertphillips391395d2016-03-02 09:26:36 -0800650 // Set the matrix so that rendered clip elements are transformed to mask space from clip
651 // space.
652 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
653
654 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000655 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
656 // pass must not set values outside of this bounds or stencil values outside the rect won't be
657 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800658
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700660 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000661 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000662 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000663 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000664 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips391395d2016-03-02 09:26:36 -0800665#ifdef SK_DEBUG
666 GrPathRenderer* pr = GetPathRenderer(context,
robertphillips68737822015-10-29 12:12:21 -0700667 texture, translate, element);
robertphillips544b9aa2015-10-28 11:01:41 -0700668 if (Element::kRect_Type != element->getType() && !pr) {
robertphillips391395d2016-03-02 09:26:36 -0800669 // UseSWOnlyPath should now filter out all cases where gpu-side mask merging would
670 // be performed (i.e., pr would be NULL for a non-rect path).
671 // See https://bug.skia.org/4519 for rationale and details.
robertphillips544b9aa2015-10-28 11:01:41 -0700672 SkASSERT(0);
robertphillips391395d2016-03-02 09:26:36 -0800673 }
674#endif
675
676 // draw directly into the result with the stencil set to make the pixels affected
677 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700678 static constexpr GrUserStencilSettings kStencilInElement(
679 GrUserStencilSettings::StaticInit<
680 0xffff,
681 GrUserStencilTest::kAlways,
682 0xffff,
683 GrUserStencilOp::kReplace,
684 GrUserStencilOp::kReplace,
685 0xffff>()
686 );
687 if (!stencil_element(dc.get(), &maskSpaceIBounds, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800688 translate, element)) {
689 texture->resourcePriv().removeUniqueKey();
690 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000691 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000692
robertphillips391395d2016-03-02 09:26:36 -0800693 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700694 static constexpr GrUserStencilSettings kDrawOutsideElement(
695 GrUserStencilSettings::StaticInit<
696 0x0000,
697 GrUserStencilTest::kEqual,
698 0xffff,
699 GrUserStencilOp::kZero,
700 GrUserStencilOp::kZero,
701 0xffff>()
702 );
703 if (!dc->drawContextPriv().drawAndStencilRect(&maskSpaceIBounds, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800704 op, !invert, false,
705 translate,
706 SkRect::Make(clipSpaceIBounds))) {
707 texture->resourcePriv().removeUniqueKey();
708 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000709 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000710 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800711 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800712 GrPaint paint;
713 paint.setAntiAlias(element->isAA());
714 paint.setCoverageSetOpXPFactory(op, false);
715
robertphillips6c7e3252016-04-27 10:47:51 -0700716 draw_element(dc.get(), GrClip::WideOpen(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000717 }
718 }
719
mtklein18300a32016-03-16 13:53:35 -0700720 return texture.release();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000721}
722
723////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000724// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000725// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800726bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
727 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700728 GrReducedClip::InitialState initialState,
729 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000730 const SkIRect& clipSpaceIBounds,
731 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700732 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733
robertphillips544b9aa2015-10-28 11:01:41 -0700734 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700735 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 return false;
737 }
738
egdaniel8dc7c3a2015-04-16 11:22:42 -0700739 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
740 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000741 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
742 SkVector translate = {
743 SkIntToScalar(clipSpaceToStencilOffset.fX),
744 SkIntToScalar(clipSpaceToStencilOffset.fY)
745 };
joshualitt8059eb92014-12-29 15:10:07 -0800746 SkMatrix viewMatrix;
747 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748
bsalomon@google.com9f131742012-12-13 20:43:56 +0000749 // We set the current clip to the bounds so that our recursive draws are scissored to them.
750 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
751 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800752 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753
bsalomonb3b9aec2015-09-10 11:16:35 -0700754 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
755 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
757 // walk through each clip element and perform its set op
758 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700759 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000760 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800761
egdaniel8dd688b2015-01-22 10:16:09 -0800762 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800763 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800764 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800765
egdaniel8dd688b2015-01-22 10:16:09 -0800766 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800767
768 // if the target is MSAA then we want MSAA enabled when the clip is soft
cdaltonede75742015-11-11 15:27:57 -0800769 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700770 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800771 }
772
tomhudson@google.com8afae612012-08-14 15:03:35 +0000773 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700775 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000777 // This will be used to determine whether the clip shape can be rendered into the
778 // stencil with arbitrary stencil settings.
779 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000781 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000782
halcanary96fcdcc2015-08-27 07:41:13 -0700783 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000784 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000785 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000786 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000788 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000789 element->asPath(&clipPath);
790 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000791 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000792 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000793 }
robertphillips68737822015-10-29 12:12:21 -0700794
cdalton93a379b2016-05-11 13:58:08 -0700795 SkASSERT(!pipelineBuilder.hasUserStencilSettings());
robertphillips68737822015-10-29 12:12:21 -0700796
797 GrPathRenderer::CanDrawPathArgs canDrawArgs;
798 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
799 canDrawArgs.fViewMatrix = &viewMatrix;
800 canDrawArgs.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700801 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700802 canDrawArgs.fAntiAlias = false;
cdalton93a379b2016-05-11 13:58:08 -0700803 canDrawArgs.fHasUserStencilSettings = pipelineBuilder.hasUserStencilSettings();
robertphillips68737822015-10-29 12:12:21 -0700804 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
805
806 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
807 GrPathRendererChain::kStencilOnly_DrawType,
808 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700809 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 return false;
811 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 }
813
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000814 bool canRenderDirectToStencil =
815 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700816 bool drawDirectToClip; // Given the renderer, the element,
817 // fill rule, and set operation should
818 // we render the element directly to
819 // stencil bit used for clipping.
820 GrUserStencilSettings const* const* stencilPasses =
821 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
822 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823
824 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700825 if (!drawDirectToClip) {
826 static constexpr GrUserStencilSettings kDrawToStencil(
827 GrUserStencilSettings::StaticInit<
828 0x0000,
829 GrUserStencilTest::kAlways,
830 0xffff,
831 GrUserStencilOp::kIncMaybeClamp,
832 GrUserStencilOp::kIncMaybeClamp,
833 0xffff>()
834 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000835 if (Element::kRect_Type == element->getType()) {
cdalton93a379b2016-05-11 13:58:08 -0700836 pipelineBuilder.setUserStencil(&kDrawToStencil);
joshualitt73bb4562015-03-25 07:16:21 -0700837
joshualitta8b84992016-01-13 13:35:35 -0800838 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
839 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000841 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000842 if (canRenderDirectToStencil) {
cdalton93a379b2016-05-11 13:58:08 -0700843 pipelineBuilder.setUserStencil(&kDrawToStencil);
bsalomon0aff2fa2015-07-31 06:48:27 -0700844
845 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700846 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700847 args.fResourceProvider = this->getContext()->resourceProvider();
848 args.fPipelineBuilder = &pipelineBuilder;
849 args.fColor = GrColor_WHITE;
850 args.fViewMatrix = &viewMatrix;
851 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700852 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700853 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700854 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700855 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000856 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700857 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700858 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700859 args.fResourceProvider = this->getContext()->resourceProvider();
860 args.fPipelineBuilder = &pipelineBuilder;
861 args.fViewMatrix = &viewMatrix;
862 args.fPath = &clipPath;
bsalomon0aff2fa2015-07-31 06:48:27 -0700863 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000864 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 }
866 }
867 }
868
869 // now we modify the clip bit by rendering either the clip
870 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700871 fClipMode = kModifyClip_StencilClipMode;
cdalton93a379b2016-05-11 13:58:08 -0700872 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
873 pipelineBuilder.setUserStencil(*pass);
joshualitt9853cce2014-11-17 14:22:48 -0800874
cdalton93a379b2016-05-11 13:58:08 -0700875 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000876 if (Element::kRect_Type == element->getType()) {
joshualitta8b84992016-01-13 13:35:35 -0800877 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
878 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000879 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700880 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700881 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700882 args.fResourceProvider = this->getContext()->resourceProvider();
883 args.fPipelineBuilder = &pipelineBuilder;
884 args.fColor = GrColor_WHITE;
885 args.fViewMatrix = &viewMatrix;
886 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700887 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700888 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700889 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700890 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000891 }
892 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000893 // The view matrix is setup to do clip space -> stencil space translation, so
894 // draw rect in clip space.
joshualitta8b84992016-01-13 13:35:35 -0800895 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
896 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000897 }
898 }
899 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000900 }
joshualitt7a6184f2014-10-29 18:29:27 -0700901 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000902 return true;
903}
904
bsalomon@google.com411dad02012-06-05 20:24:20 +0000905////////////////////////////////////////////////////////////////////////////////
robertphillips391395d2016-03-02 09:26:36 -0800906GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context,
907 int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000908 GrReducedClip::InitialState initialState,
909 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800910 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000911 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700912 GrUniqueKey key;
913 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips391395d2016-03-02 09:26:36 -0800914 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700915 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
916 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000917 }
918
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000919 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
920 // the top left corner of the resulting rect to the top left of the texture.
921 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
922
robertphillips391395d2016-03-02 09:26:36 -0800923 GrSWMaskHelper helper(context);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000924
joshualitt8059eb92014-12-29 15:10:07 -0800925 // Set the matrix so that rendered clip elements are transformed to mask space from clip
926 // space.
927 SkMatrix translate;
928 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800929
joshualitt8059eb92014-12-29 15:10:07 -0800930 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -0700931 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000932
tfarinabf54e492014-10-23 17:47:18 -0700933 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000934 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000935 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000936
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
938 // Intersect and reverse difference require modifying pixels outside of the geometry
939 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
940 // but leave the pixels inside the geometry alone. For reverse difference we invert all
941 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000942 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000943 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000944 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000945 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000946 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000947 SkPath clipPath;
948 element->asPath(&clipPath);
949 clipPath.toggleInverseFillType();
bsalomon6663acf2016-05-10 09:14:17 -0700950 helper.draw(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op, element->isAA(),
951 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000952 continue;
953 }
954
955 // The other ops (union, xor, diff) only affect pixels inside
956 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000957 if (Element::kRect_Type == element->getType()) {
958 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
959 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000960 SkPath path;
961 element->asPath(&path);
bsalomon6663acf2016-05-10 09:14:17 -0700962 helper.draw(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000963 }
964 }
965
krajcevskiad1dc582014-06-10 15:06:47 -0700966 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800967 GrSurfaceDesc desc;
968 desc.fWidth = clipSpaceIBounds.width();
969 desc.fHeight = clipSpaceIBounds.height();
970 desc.fConfig = kAlpha_8_GrPixelConfig;
971
972 GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0);
973 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700974 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700975 }
robertphillips391395d2016-03-02 09:26:36 -0800976 result->resourcePriv().setUniqueKey(key);
977
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +0000978 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000979
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000980 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000981}