blob: 654b315d527961b95a51403098d718656e484e67 [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,
65 bool isStencilDisabled,
66 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 }
88 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
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;
106 canDrawArgs.fStroke = &stroke;
107 canDrawArgs.fAntiAlias = element->isAA();
108 canDrawArgs.fIsStencilDisabled = isStencilDisabled;
109 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;
robertphillips423e3372015-10-27 09:23:38 -0700128 static const bool kNeedsStencil = true;
robertphillips68737822015-10-29 12:12:21 -0700129 static const bool kStencilIsDisabled = true;
130 PathNeedsSWRenderer(context,
131 kStencilIsDisabled,
132 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
bsalomon69cfe952015-11-30 13:27:47 -0800140GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget, bool debugClipBatchToBounds)
bsalomonc988d2c2015-10-07 09:30:05 -0700141 : fDrawTarget(drawTarget)
bsalomon69cfe952015-11-30 13:27:47 -0800142 , fClipMode(kIgnoreClip_StencilClipMode)
143 , fDebugClipBatchToBounds(debugClipBatchToBounds) {
bsalomonedd77a12015-05-29 09:45:57 -0700144}
145
robertphillips544b9aa2015-10-28 11:01:41 -0700146GrContext* GrClipMaskManager::getContext() {
147 return fDrawTarget->cmmAccess().context();
148}
bsalomonedd77a12015-05-29 09:45:57 -0700149
robertphillips544b9aa2015-10-28 11:01:41 -0700150const GrCaps* GrClipMaskManager::caps() const {
151 return fDrawTarget->caps();
152}
153
154GrResourceProvider* GrClipMaskManager::resourceProvider() {
155 return fDrawTarget->cmmAccess().resourceProvider();
156}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000157/*
158 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
159 * will be used on any element. If so, it returns true to indicate that the
160 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
161 */
robertphillips391395d2016-03-02 09:26:36 -0800162bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
163 const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700164 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800165 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800166 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000167 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000168 // a clip gets complex enough it can just be done in SW regardless
169 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000170
joshualitt8059eb92014-12-29 15:10:07 -0800171 // Set the matrix so that rendered clip elements are transformed to mask space from clip
172 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700173 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800174
tfarinabf54e492014-10-23 17:47:18 -0700175 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000176 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700177
178 SkRegion::Op op = element->getOp();
179 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700180 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700181 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700182
robertphillips391395d2016-03-02 09:26:36 -0800183 if (PathNeedsSWRenderer(context, pipelineBuilder.getStencil().isDisabled(),
robertphillips68737822015-10-29 12:12:21 -0700184 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700185 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000186 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000187 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000188 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000189}
190
bsalomon0b5b6b22015-10-14 08:31:34 -0700191bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700192 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700193 SkVector& clipToRTOffset,
194 const SkRect* drawBounds,
195 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700197 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 boundsInClipSpace = *drawBounds;
199 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
200 }
bsalomon0ba8c242015-10-07 09:20:28 -0700201 SkASSERT(elements.count() <= kMaxAnalyticElements);
202 const GrFragmentProcessor* fps[kMaxAnalyticElements];
203 for (int i = 0; i < kMaxAnalyticElements; ++i) {
204 fps[i] = nullptr;
205 }
206 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700207 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700209 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000210 SkRegion::Op op = iter.get()->getOp();
211 bool invert;
212 bool skip = false;
213 switch (op) {
214 case SkRegion::kReplace_Op:
215 SkASSERT(iter.get() == elements.head());
216 // Fallthrough, handled same as intersect.
217 case SkRegion::kIntersect_Op:
218 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700219 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000220 skip = true;
221 }
222 break;
223 case SkRegion::kDifference_Op:
224 invert = true;
225 // We don't currently have a cheap test for whether a rect is fully outside an
226 // element's primitive, so don't attempt to set skip.
227 break;
228 default:
229 failed = true;
230 break;
231 }
232 if (failed) {
233 break;
234 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700236 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800237 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700238 if (abortIfAA) {
239 failed = true;
240 break;
241 }
joshualittb0a8a372014-09-23 09:50:21 -0700242 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700243 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000244 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700245 edgeType =
246 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000247 }
bsalomona912dde2015-10-14 15:01:50 -0700248
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000249 switch (iter.get()->getType()) {
250 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700251 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
252 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 break;
254 case SkClipStack::Element::kRRect_Type: {
255 SkRRect rrect = iter.get()->getRRect();
256 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700257 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000258 break;
259 }
260 case SkClipStack::Element::kRect_Type: {
261 SkRect rect = iter.get()->getRect();
262 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700263 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 break;
265 }
266 default:
267 break;
268 }
bsalomon0ba8c242015-10-07 09:20:28 -0700269 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 failed = true;
271 break;
272 }
bsalomon0ba8c242015-10-07 09:20:28 -0700273 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274 }
mtklein217daa72014-07-02 12:55:21 -0700275 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000276 }
277
bsalomon0b5b6b22015-10-14 08:31:34 -0700278 *resultFP = nullptr;
279 if (!failed && fpCnt) {
280 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000281 }
bsalomon0ba8c242015-10-07 09:20:28 -0700282 for (int i = 0; i < fpCnt; ++i) {
283 fps[i]->unref();
284 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700285 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000286}
287
bsalomon69cfe952015-11-30 13:27:47 -0800288static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) {
289 switch (clip.clipType()) {
290 case GrClip::kClipStack_ClipType: {
291 SkClipStack* stack = new SkClipStack;
292 *stack = *clip.clipStack();
293 // The stack is actually in clip space not device space.
294 SkRect clipRect = devRect;
295 SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) };
296 clipRect.offset(origin);
297 SkIRect iclipRect;
298 clipRect.roundOut(&iclipRect);
299 clipRect = SkRect::Make(iclipRect);
300 stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false);
301 out->setClipStack(stack, &clip.origin());
302 break;
303 }
304 case GrClip::kWideOpen_ClipType:
305 *out = GrClip(devRect);
306 break;
307 case GrClip::kIRect_ClipType: {
308 SkIRect intersect;
309 devRect.roundOut(&intersect);
310 if (intersect.intersect(clip.irect())) {
311 *out = GrClip(intersect);
312 } else {
313 *out = clip;
314 }
315 break;
316 }
317 }
318}
319
robertphillips391395d2016-03-02 09:26:36 -0800320bool GrClipMaskManager::setupScissorClip(const GrPipelineBuilder& pipelineBuilder,
321 GrPipelineBuilder::AutoRestoreStencil* ars,
322 const SkIRect& clipScissor,
323 const SkRect* devBounds,
324 GrAppliedClip* out) {
325 if (kRespectClip_StencilClipMode == fClipMode) {
326 fClipMode = kIgnoreClip_StencilClipMode;
327 }
328
329 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
330
331 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
332 SkIRect devBoundsScissor;
333 const SkIRect* scissor = &clipScissor;
334 bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds;
335 if (doDevBoundsClip) {
336 devBounds->roundOut(&devBoundsScissor);
337 if (devBoundsScissor.intersect(clipScissor)) {
338 scissor = &devBoundsScissor;
339 }
340 }
341
342 if (scissor->contains(clipSpaceRTIBounds)) {
343 // This counts as wide open
344 this->setPipelineBuilderStencil(pipelineBuilder, ars);
345 return true;
346 }
347
348 if (clipSpaceRTIBounds.intersect(*scissor)) {
349 out->fScissorState.set(clipSpaceRTIBounds);
350 this->setPipelineBuilderStencil(pipelineBuilder, ars);
351 return true;
352 }
353 return false;
354}
355
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000357// sort out what kind of clip mask needs to be created: alpha, stencil,
358// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700359bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800360 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon0ba8c242015-10-07 09:20:28 -0700361 const SkRect* devBounds,
362 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700363 if (kRespectClip_StencilClipMode == fClipMode) {
364 fClipMode = kIgnoreClip_StencilClipMode;
365 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000366
bsalomonf045d602015-11-18 19:01:12 -0800367 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800368 int32_t genID = 0;
369 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000370 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800371 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700372 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000373
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700375 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000376
joshualitt44701df2015-02-23 14:44:57 -0800377 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon69cfe952015-11-30 13:27:47 -0800378 GrClip devBoundsClip;
379 bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds;
380 if (doDevBoundsClip) {
381 add_rect_to_clip(pipelineBuilder.clip(), *devBounds, &devBoundsClip);
382 }
383 const GrClip& clip = doDevBoundsClip ? devBoundsClip : pipelineBuilder.clip();
384
bsalomon96e02a82015-03-06 07:13:01 -0800385 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800386 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000387 return true;
388 }
389
bsalomon96e02a82015-03-06 07:13:01 -0800390 // The clip mask manager always draws with a single IRect so we special case that logic here
391 // Image filters just use a rect, so we also special case that logic
392 switch (clip.clipType()) {
393 case GrClip::kWideOpen_ClipType:
394 SkFAIL("Should have caught this with clip.isWideOpen()");
395 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800396 case GrClip::kIRect_ClipType: {
397 SkIRect scissor = clip.irect();
398 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700399 out->fScissorState.set(scissor);
bsalomon9ce30e12015-03-06 08:42:34 -0800400 this->setPipelineBuilderStencil(pipelineBuilder, ars);
401 return true;
402 }
403 return false;
404 }
bsalomon96e02a82015-03-06 07:13:01 -0800405 case GrClip::kClipStack_ClipType: {
406 clipSpaceRTIBounds.offset(clip.origin());
bsalomondb4758c2015-11-23 11:14:20 -0800407 SkIRect clipSpaceReduceQueryBounds;
bsalomon5b592e82016-03-09 09:55:55 -0800408#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
Brian Salomon362c9002015-11-30 17:02:50 -0500409 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
bsalomondb4758c2015-11-23 11:14:20 -0800410 SkIRect devIBounds = devBounds->roundOut();
411 devIBounds.offset(clip.origin());
412 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
413 return false;
414 }
415 } else {
416 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
417 }
bsalomon96e02a82015-03-06 07:13:01 -0800418 GrReducedClip::ReduceClipStack(*clip.clipStack(),
bsalomondb4758c2015-11-23 11:14:20 -0800419 clipSpaceReduceQueryBounds,
bsalomon96e02a82015-03-06 07:13:01 -0800420 &elements,
421 &genID,
422 &initialState,
423 &clipSpaceIBounds,
424 &requiresAA);
425 if (elements.isEmpty()) {
426 if (GrReducedClip::kAllIn_InitialState == initialState) {
427 if (clipSpaceIBounds == clipSpaceRTIBounds) {
428 this->setPipelineBuilderStencil(pipelineBuilder, ars);
429 return true;
430 }
431 } else {
432 return false;
433 }
434 }
435 } break;
436 }
437
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000438 // An element count of 4 was chosen because of the common pattern in Blink of:
439 // isect RR
440 // diff RR
441 // isect convex_poly
442 // isect convex_poly
443 // when drawing rounded div borders. This could probably be tuned based on a
444 // configuration's relative costs of switching RTs to generate a mask vs
445 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700446 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800447 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
448 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800449 // When there are multiple samples we want to do per-sample clipping, not compute a
450 // fractional pixel coverage.
cdalton3ccf2e72016-05-06 09:41:16 -0700451 bool disallowAnalyticAA = rt->isStencilBufferMultisampled();
452 if (disallowAnalyticAA && !rt->numColorSamples()) {
453 // With a single color sample, any coverage info is lost from color once it hits the
454 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
455 // is multisampled.
456 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
457 !pipelineBuilder.getStencil().isDisabled();
458 }
bsalomon0ba8c242015-10-07 09:20:28 -0700459 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000460 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700461 (requiresAA &&
462 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
463 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700464 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800465 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700466 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700467 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700468 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000469 }
egdaniel8dd688b2015-01-22 10:16:09 -0800470 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700471 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000472 return true;
473 }
474 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000475
cdaltonede75742015-11-11 15:27:57 -0800476 // If the stencil buffer is multisampled we can use it to do everything.
477 if (!rt->isStencilBufferMultisampled() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700478 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479
joshualitt8059eb92014-12-29 15:10:07 -0800480 // The top-left of the mask corresponds to the top-left corner of the bounds.
481 SkVector clipToMaskOffset = {
482 SkIntToScalar(-clipSpaceIBounds.fLeft),
483 SkIntToScalar(-clipSpaceIBounds.fTop)
484 };
485
robertphillips391395d2016-03-02 09:26:36 -0800486 if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000487 // The clip geometry is complex enough that it will be more efficient to create it
488 // entirely in software
robertphillips391395d2016-03-02 09:26:36 -0800489 result.reset(CreateSoftwareClipMask(this->getContext(),
490 genID,
491 initialState,
492 elements,
493 clipToMaskOffset,
494 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000495 } else {
robertphillips391395d2016-03-02 09:26:36 -0800496 result.reset(CreateAlphaClipMask(this->getContext(),
497 genID,
498 initialState,
499 elements,
500 clipToMaskOffset,
501 clipSpaceIBounds));
502 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700503 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000504 }
505
bsalomon49f085d2014-09-05 13:34:00 -0700506 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000507 // The mask's top left coord should be pinned to the rounded-out top left corner of
508 // clipSpace bounds. We determine the mask's position WRT to the render target here.
509 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800510 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700511 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800512 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000513 return true;
514 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000518 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800519 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800520 this->createStencilClipMask(rt,
521 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000522 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 elements,
524 clipSpaceIBounds,
525 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000526
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
528 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
529 // use both stencil and scissor test to the bounds for the final draw.
530 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
531 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700532 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800533 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000534 return true;
535}
536
robertphillips391395d2016-03-02 09:26:36 -0800537static bool stencil_element(GrDrawContext* dc,
538 const SkIRect* scissorRect,
539 const GrStencilSettings& ss,
540 const SkMatrix& viewMatrix,
541 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800542
543 // TODO: Draw rrects directly here.
544 switch (element->getType()) {
545 case Element::kEmpty_Type:
546 SkDEBUGFAIL("Should never get here with an empty element.");
547 break;
robertphillips391395d2016-03-02 09:26:36 -0800548 case Element::kRect_Type:
549 return dc->drawContextPriv().drawAndStencilRect(scissorRect, ss,
550 element->getOp(),
551 element->isInverseFilled(),
552 element->isAA(),
553 viewMatrix, element->getRect());
554 break;
robertphillips86c60752016-03-02 08:43:13 -0800555 default: {
556 SkPath path;
557 element->asPath(&path);
558 if (path.isInverseFillType()) {
559 path.toggleInverseFillType();
560 }
561
robertphillips391395d2016-03-02 09:26:36 -0800562 return dc->drawContextPriv().drawAndStencilPath(scissorRect, ss,
563 element->getOp(),
564 element->isInverseFilled(),
565 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800566 break;
567 }
568 }
robertphillips391395d2016-03-02 09:26:36 -0800569
570 return false;
571}
572
573static void draw_element(GrDrawContext* dc,
574 const GrClip& clip, // TODO: can this just always be WideOpen?
575 const GrPaint &paint,
576 const SkMatrix& viewMatrix,
577 const SkClipStack::Element* element) {
578
579 // TODO: Draw rrects directly here.
580 switch (element->getType()) {
581 case Element::kEmpty_Type:
582 SkDEBUGFAIL("Should never get here with an empty element.");
583 break;
584 case Element::kRect_Type:
585 dc->drawRect(clip, paint, viewMatrix, element->getRect());
586 break;
587 default: {
588 SkPath path;
589 element->asPath(&path);
590 if (path.isInverseFillType()) {
591 path.toggleInverseFillType();
592 }
593
594 dc->drawPath(clip, paint, viewMatrix, path, GrStrokeInfo::FillInfo());
595 break;
596 }
597 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598}
599
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000600////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700601// Create a 8-bit clip mask in alpha
602
603static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
604 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
605 GrUniqueKey::Builder builder(key, kDomain, 3);
606 builder[0] = clipGenID;
607 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
608 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
609}
610
robertphillips391395d2016-03-02 09:26:36 -0800611GrTexture* GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
612 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700613 GrReducedClip::InitialState initialState,
614 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800615 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800617 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700618 GrUniqueKey key;
619 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
620 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700621 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000622 }
623
robertphillips544b9aa2015-10-28 11:01:41 -0700624 // There's no texture in the cache. Let's try to allocate it then.
robertphillips391395d2016-03-02 09:26:36 -0800625 GrSurfaceDesc desc;
626 desc.fWidth = clipSpaceIBounds.width();
627 desc.fHeight = clipSpaceIBounds.height();
628 desc.fFlags = kRenderTarget_GrSurfaceFlag;
629 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
630 desc.fConfig = kAlpha_8_GrPixelConfig;
631 } else {
632 desc.fConfig = kRGBA_8888_GrPixelConfig;
633 }
634
635 SkAutoTUnref<GrTexture> texture(resourceProvider->createApproxTexture(desc, 0));
bsalomon473addf2015-10-02 07:49:05 -0700636 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700637 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638 }
639
robertphillips391395d2016-03-02 09:26:36 -0800640 texture->resourcePriv().setUniqueKey(key);
641
robertphillips6c7e3252016-04-27 10:47:51 -0700642 sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(texture->asRenderTarget())));
robertphillips391395d2016-03-02 09:26:36 -0800643 if (!dc) {
644 return nullptr;
645 }
joshualitt8059eb92014-12-29 15:10:07 -0800646
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000647 // The texture may be larger than necessary, this rect represents the part of the texture
648 // we populate with a rasterization of the clip.
649 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
650
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000651 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
652 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800653 dc->clear(&maskSpaceIBounds,
654 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
655 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000656
robertphillips391395d2016-03-02 09:26:36 -0800657 // Set the matrix so that rendered clip elements are transformed to mask space from clip
658 // space.
659 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
660
661 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000662 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
663 // pass must not set values outside of this bounds or stencil values outside the rect won't be
664 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800665
robertphillips@google.comf294b772012-04-27 14:29:26 +0000666 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700667 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000668 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000669 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000670 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000671 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips391395d2016-03-02 09:26:36 -0800672#ifdef SK_DEBUG
673 GrPathRenderer* pr = GetPathRenderer(context,
robertphillips68737822015-10-29 12:12:21 -0700674 texture, translate, element);
robertphillips544b9aa2015-10-28 11:01:41 -0700675 if (Element::kRect_Type != element->getType() && !pr) {
robertphillips391395d2016-03-02 09:26:36 -0800676 // UseSWOnlyPath should now filter out all cases where gpu-side mask merging would
677 // be performed (i.e., pr would be NULL for a non-rect path).
678 // See https://bug.skia.org/4519 for rationale and details.
robertphillips544b9aa2015-10-28 11:01:41 -0700679 SkASSERT(0);
robertphillips391395d2016-03-02 09:26:36 -0800680 }
681#endif
682
683 // draw directly into the result with the stencil set to make the pixels affected
684 // by the clip shape be non-zero.
bsalomon3de75da2016-04-29 08:44:16 -0700685 static constexpr GrStencilSettings kStencilInElement(
686 kReplace_StencilOp,
687 kReplace_StencilOp,
688 kAlways_StencilFunc,
689 0xffff,
690 0xffff,
691 0xffff);
robertphillips6c7e3252016-04-27 10:47:51 -0700692 if (!stencil_element(dc.get(), &maskSpaceIBounds, kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800693 translate, element)) {
694 texture->resourcePriv().removeUniqueKey();
695 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000696 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000697
robertphillips391395d2016-03-02 09:26:36 -0800698 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon3de75da2016-04-29 08:44:16 -0700699 static constexpr GrStencilSettings kDrawOutsideElement(
700 kZero_StencilOp,
701 kZero_StencilOp,
702 kEqual_StencilFunc,
703 0xffff,
704 0x0000,
705 0xffff);
robertphillips391395d2016-03-02 09:26:36 -0800706 if (!dc->drawContextPriv().drawAndStencilRect(&maskSpaceIBounds, kDrawOutsideElement,
707 op, !invert, false,
708 translate,
709 SkRect::Make(clipSpaceIBounds))) {
710 texture->resourcePriv().removeUniqueKey();
711 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000712 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000713 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800714 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800715 GrPaint paint;
716 paint.setAntiAlias(element->isAA());
717 paint.setCoverageSetOpXPFactory(op, false);
718
robertphillips6c7e3252016-04-27 10:47:51 -0700719 draw_element(dc.get(), GrClip::WideOpen(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000720 }
721 }
722
mtklein18300a32016-03-16 13:53:35 -0700723 return texture.release();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000724}
725
726////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000727// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000728// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800729bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
730 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700731 GrReducedClip::InitialState initialState,
732 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000733 const SkIRect& clipSpaceIBounds,
734 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700735 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
robertphillips544b9aa2015-10-28 11:01:41 -0700737 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700738 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 return false;
740 }
741
egdaniel8dc7c3a2015-04-16 11:22:42 -0700742 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
743 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000744 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
745 SkVector translate = {
746 SkIntToScalar(clipSpaceToStencilOffset.fX),
747 SkIntToScalar(clipSpaceToStencilOffset.fY)
748 };
joshualitt8059eb92014-12-29 15:10:07 -0800749 SkMatrix viewMatrix;
750 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751
bsalomon@google.com9f131742012-12-13 20:43:56 +0000752 // We set the current clip to the bounds so that our recursive draws are scissored to them.
753 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
754 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800755 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
egdaniel8dc7c3a2015-04-16 11:22:42 -0700757 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000758 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 clipBit = (1 << (clipBit-1));
760
bsalomonb3b9aec2015-09-10 11:16:35 -0700761 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
762 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763
764 // walk through each clip element and perform its set op
765 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700766 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000767 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800768
egdaniel8dd688b2015-01-22 10:16:09 -0800769 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800770 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800771 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800772
egdaniel8dd688b2015-01-22 10:16:09 -0800773 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800774
775 // if the target is MSAA then we want MSAA enabled when the clip is soft
cdaltonede75742015-11-11 15:27:57 -0800776 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700777 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800778 }
779
tomhudson@google.com8afae612012-08-14 15:03:35 +0000780 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700782 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000784 // This will be used to determine whether the clip shape can be rendered into the
785 // stencil with arbitrary stencil settings.
786 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787
kkinnunen18996512015-04-26 23:18:49 -0700788 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000789 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000790
halcanary96fcdcc2015-08-27 07:41:13 -0700791 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000792 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000793 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000794 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000796 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000797 element->asPath(&clipPath);
798 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000799 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000800 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000801 }
robertphillips68737822015-10-29 12:12:21 -0700802
803 SkASSERT(pipelineBuilder.getStencil().isDisabled());
804
805 GrPathRenderer::CanDrawPathArgs canDrawArgs;
806 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
807 canDrawArgs.fViewMatrix = &viewMatrix;
808 canDrawArgs.fPath = &clipPath;
809 canDrawArgs.fStroke = &stroke;
810 canDrawArgs.fAntiAlias = false;
811 canDrawArgs.fIsStencilDisabled = pipelineBuilder.getStencil().isDisabled();
812 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
813
814 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
815 GrPathRendererChain::kStencilOnly_DrawType,
816 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700817 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 return false;
819 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000820 }
821
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 int passes;
823 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
824
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000825 bool canRenderDirectToStencil =
826 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000828 // fill rule, and set operation can
829 // we render the element directly to
830 // stencil bit used for clipping.
831 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
832 canRenderDirectToStencil,
833 clipBit,
834 fillInverted,
835 &passes,
836 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000837
838 // draw the element to the client stencil bits if necessary
839 if (!canDrawDirectToClip) {
bsalomon3de75da2016-04-29 08:44:16 -0700840 static constexpr GrStencilSettings kDrawToStencil(
841 kIncClamp_StencilOp,
842 kIncClamp_StencilOp,
843 kAlways_StencilFunc,
844 0xffff,
845 0x0000,
846 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000847 if (Element::kRect_Type == element->getType()) {
bsalomon3de75da2016-04-29 08:44:16 -0700848 *pipelineBuilder.stencil() = kDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700849
joshualitta8b84992016-01-13 13:35:35 -0800850 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
851 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000853 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000854 if (canRenderDirectToStencil) {
bsalomon3de75da2016-04-29 08:44:16 -0700855 *pipelineBuilder.stencil() = kDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700856
857 GrPathRenderer::DrawPathArgs 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.fColor = GrColor_WHITE;
862 args.fViewMatrix = &viewMatrix;
863 args.fPath = &clipPath;
864 args.fStroke = &stroke;
865 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700866 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700867 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000868 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700869 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700870 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700871 args.fResourceProvider = this->getContext()->resourceProvider();
872 args.fPipelineBuilder = &pipelineBuilder;
873 args.fViewMatrix = &viewMatrix;
874 args.fPath = &clipPath;
bsalomon0aff2fa2015-07-31 06:48:27 -0700875 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000876 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000877 }
878 }
879 }
880
881 // now we modify the clip bit by rendering either the clip
882 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700883 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700885 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800886
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000887 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000888 if (Element::kRect_Type == element->getType()) {
joshualitta8b84992016-01-13 13:35:35 -0800889 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
890 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000891 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700892 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700893 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700894 args.fResourceProvider = this->getContext()->resourceProvider();
895 args.fPipelineBuilder = &pipelineBuilder;
896 args.fColor = GrColor_WHITE;
897 args.fViewMatrix = &viewMatrix;
898 args.fPath = &clipPath;
899 args.fStroke = &stroke;
900 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700901 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700902 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000903 }
904 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000905 // The view matrix is setup to do clip space -> stencil space translation, so
906 // draw rect in clip space.
joshualitta8b84992016-01-13 13:35:35 -0800907 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
908 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000909 }
910 }
911 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000912 }
joshualitt7a6184f2014-10-29 18:29:27 -0700913 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000914 return true;
915}
916
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917// mapping of clip-respecting stencil funcs to normal stencil funcs
918// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000919static const GrStencilFunc
bsalomon3de75da2016-04-29 08:44:16 -0700920 gSpecialToBasicStencilFunc[2][kClipStencilFuncCnt] = {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000921 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
922 // In the Clip Funcs
923 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
924 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
925 kLess_StencilFunc, // kLessIfInClip_StencilFunc
926 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
927 // Special in the clip func that forces user's ref to be 0.
928 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
929 // make ref 0 and do normal nequal.
930 },
931 {// Stencil-Clipping is ENABLED
932 // In the Clip Funcs
933 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
934 // eq stencil clip bit, mask
935 // out user bits.
936
937 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
938 // add stencil bit to mask and ref
939
940 kLess_StencilFunc, // kLessIfInClip_StencilFunc
941 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
942 // for both of these we can add
943 // the clip bit to the mask and
944 // ref and compare as normal
945 // Special in the clip func that forces user's ref to be 0.
946 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
947 // make ref have only the clip bit set
948 // and make comparison be less
949 // 10..0 < 1..user_bits..
950 }
951};
952
joshualitt5e6ba212015-07-13 07:35:05 -0700953void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800954 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 // We make two copies of the StencilSettings here (except in the early
956 // exit scenario. One copy from draw state to the stack var. Then another
957 // from the stack var to the gpu. We could make this class hold a ptr to
958 // GrGpu's fStencilSettings and eliminate the stack copy here.
959
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 // use stencil for clipping if clipping is enabled and the clip
961 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800963
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 // The GrGpu client may not be using the stencil buffer but we may need to
965 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700966 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700967 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon3de75da2016-04-29 08:44:16 -0700968 static constexpr GrStencilSettings kBasicApplyClipSettings(
969 kKeep_StencilOp,
970 kKeep_StencilOp,
971 kAlwaysIfInClip_StencilFunc,
972 0x0000,
973 0x0000,
974 0x0000);
975 settings = kBasicApplyClipSettings;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977 return;
978 }
979 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700980 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000981 }
982
bsalomon@google.coma3201942012-06-21 19:58:20 +0000983 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700984 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
robertphillips544b9aa2015-10-28 11:01:41 -0700985 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700986 if (stencilAttachment) {
987 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000988 }
989
robertphillips544b9aa2015-10-28 11:01:41 -0700990 SkASSERT(this->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
991 SkASSERT(this->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700992 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700993 ars->set(&pipelineBuilder);
994 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995}
996
997void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
998 StencilClipMode mode,
999 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001000 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001001
1002 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003 // We assume that this clip manager itself is drawing to the GrGpu and
1004 // has already setup the correct values.
1005 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001007
bsalomon@google.com411dad02012-06-05 20:24:20 +00001008 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1009 unsigned int userBits = clipBit - 1;
1010
bsalomon@google.coma3201942012-06-21 19:58:20 +00001011 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
robertphillips544b9aa2015-10-28 11:01:41 -07001012 bool twoSided = this->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 bool finished = false;
1015 while (!finished) {
1016 GrStencilFunc func = settings->func(face);
1017 uint16_t writeMask = settings->writeMask(face);
1018 uint16_t funcMask = settings->funcMask(face);
1019 uint16_t funcRef = settings->funcRef(face);
1020
bsalomon3de75da2016-04-29 08:44:16 -07001021 SkASSERT((unsigned) func < kStencilFuncCnt);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022
1023 writeMask &= userBits;
1024
bsalomon3de75da2016-04-29 08:44:16 -07001025 if (func >= kBasicStencilFuncCnt) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001026 int respectClip = kRespectClip_StencilClipMode == mode;
1027 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001028 switch (func) {
1029 case kAlwaysIfInClip_StencilFunc:
1030 funcMask = clipBit;
1031 funcRef = clipBit;
1032 break;
1033 case kEqualIfInClip_StencilFunc:
1034 case kLessIfInClip_StencilFunc:
1035 case kLEqualIfInClip_StencilFunc:
1036 funcMask = (funcMask & userBits) | clipBit;
1037 funcRef = (funcRef & userBits) | clipBit;
1038 break;
1039 case kNonZeroIfInClip_StencilFunc:
1040 funcMask = (funcMask & userBits) | clipBit;
1041 funcRef = clipBit;
1042 break;
1043 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001044 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001045 }
1046 } else {
1047 funcMask &= userBits;
1048 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001049 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001050 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001051 gSpecialToBasicStencilFunc[respectClip];
bsalomon3de75da2016-04-29 08:44:16 -07001052 func = table[func - kBasicStencilFuncCnt];
1053 SkASSERT(func >= 0 && func < kBasicStencilFuncCnt);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001054 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001055 funcMask &= userBits;
1056 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001057 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001058
1059 settings->setFunc(face, func);
1060 settings->setWriteMask(face, writeMask);
1061 settings->setFuncMask(face, funcMask);
1062 settings->setFuncRef(face, funcRef);
1063
1064 if (GrStencilSettings::kFront_Face == face) {
1065 face = GrStencilSettings::kBack_Face;
1066 finished = !twoSided;
1067 } else {
1068 finished = true;
1069 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001071 if (!twoSided) {
1072 settings->copyFrontSettingsToBack();
1073 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001074}
1075
1076////////////////////////////////////////////////////////////////////////////////
robertphillips391395d2016-03-02 09:26:36 -08001077GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context,
1078 int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001079 GrReducedClip::InitialState initialState,
1080 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001081 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001082 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001083 GrUniqueKey key;
1084 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips391395d2016-03-02 09:26:36 -08001085 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -07001086 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1087 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001088 }
1089
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001090 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1091 // the top left corner of the resulting rect to the top left of the texture.
1092 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1093
robertphillips391395d2016-03-02 09:26:36 -08001094 GrSWMaskHelper helper(context);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001095
joshualitt8059eb92014-12-29 15:10:07 -08001096 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1097 // space.
1098 SkMatrix translate;
1099 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001100
joshualitt8059eb92014-12-29 15:10:07 -08001101 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001102 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001103 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001104
tfarinabf54e492014-10-23 17:47:18 -07001105 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001106 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001107 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001108
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001109 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1110 // Intersect and reverse difference require modifying pixels outside of the geometry
1111 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1112 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1113 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001114 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001115 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001116 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001117 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001119 SkPath clipPath;
1120 element->asPath(&clipPath);
1121 clipPath.toggleInverseFillType();
1122 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001123 continue;
1124 }
1125
1126 // The other ops (union, xor, diff) only affect pixels inside
1127 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001128 if (Element::kRect_Type == element->getType()) {
1129 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1130 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001131 SkPath path;
1132 element->asPath(&path);
1133 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001134 }
1135 }
1136
krajcevskiad1dc582014-06-10 15:06:47 -07001137 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -08001138 GrSurfaceDesc desc;
1139 desc.fWidth = clipSpaceIBounds.width();
1140 desc.fHeight = clipSpaceIBounds.height();
1141 desc.fConfig = kAlpha_8_GrPixelConfig;
1142
1143 GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0);
1144 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001145 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001146 }
robertphillips391395d2016-03-02 09:26:36 -08001147 result->resourcePriv().setUniqueKey(key);
1148
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001149 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001150
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001151 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001152}
1153
robertphillips@google.comf294b772012-04-27 14:29:26 +00001154////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001155
egdaniel8dc7c3a2015-04-16 11:22:42 -07001156void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001157 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001158 if (stencilAttachment) {
1159 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001160 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001161 }
1162}