blob: 2700f56986baa50136b37c193713a1c64d0d7615 [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.
451 bool disallowAnalyticAA = rt->isUnifiedMultisampled() || pipelineBuilder.hasMixedSamples();
bsalomon0ba8c242015-10-07 09:20:28 -0700452 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000453 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700454 (requiresAA &&
455 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
456 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700457 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800458 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700459 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700460 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700461 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000462 }
egdaniel8dd688b2015-01-22 10:16:09 -0800463 this->setPipelineBuilderStencil(pipelineBuilder, ars);
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));
egdaniel8dd688b2015-01-22 10:16:09 -0800505 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000506 return true;
507 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000510
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000511 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800512 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800513 this->createStencilClipMask(rt,
514 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000515 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516 elements,
517 clipSpaceIBounds,
518 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000519
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
521 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
522 // use both stencil and scissor test to the bounds for the final draw.
523 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
524 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700525 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800526 this->setPipelineBuilderStencil(pipelineBuilder, ars);
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,
532 const GrStencilSettings& ss,
533 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
587 dc->drawPath(clip, paint, viewMatrix, path, GrStrokeInfo::FillInfo());
588 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
635 SkAutoTUnref<GrDrawContext> dc(context->drawContext(texture->asRenderTarget()));
636 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.
678 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
679 kReplace_StencilOp,
680 kReplace_StencilOp,
681 kAlways_StencilFunc,
682 0xffff,
683 0xffff,
684 0xffff)
685 if (!stencil_element(dc, &maskSpaceIBounds, kStencilInElement,
686 translate, element)) {
687 texture->resourcePriv().removeUniqueKey();
688 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000689 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000690
robertphillips391395d2016-03-02 09:26:36 -0800691 // Draw to the exterior pixels (those with a zero stencil value).
692 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
693 kZero_StencilOp,
694 kZero_StencilOp,
695 kEqual_StencilFunc,
696 0xffff,
697 0x0000,
698 0xffff);
699 if (!dc->drawContextPriv().drawAndStencilRect(&maskSpaceIBounds, kDrawOutsideElement,
700 op, !invert, false,
701 translate,
702 SkRect::Make(clipSpaceIBounds))) {
703 texture->resourcePriv().removeUniqueKey();
704 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000705 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000706 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800707 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800708 GrPaint paint;
709 paint.setAntiAlias(element->isAA());
710 paint.setCoverageSetOpXPFactory(op, false);
711
712 draw_element(dc, GrClip::WideOpen(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000713 }
714 }
715
mtklein18300a32016-03-16 13:53:35 -0700716 return texture.release();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000717}
718
719////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000720// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000721// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800722bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
723 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700724 GrReducedClip::InitialState initialState,
725 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000726 const SkIRect& clipSpaceIBounds,
727 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700728 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
robertphillips544b9aa2015-10-28 11:01:41 -0700730 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700731 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732 return false;
733 }
734
egdaniel8dc7c3a2015-04-16 11:22:42 -0700735 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
736 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000737 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
738 SkVector translate = {
739 SkIntToScalar(clipSpaceToStencilOffset.fX),
740 SkIntToScalar(clipSpaceToStencilOffset.fY)
741 };
joshualitt8059eb92014-12-29 15:10:07 -0800742 SkMatrix viewMatrix;
743 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744
bsalomon@google.com9f131742012-12-13 20:43:56 +0000745 // We set the current clip to the bounds so that our recursive draws are scissored to them.
746 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
747 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800748 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749
egdaniel8dc7c3a2015-04-16 11:22:42 -0700750 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000751 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 clipBit = (1 << (clipBit-1));
753
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
kkinnunen18996512015-04-26 23:18:49 -0700781 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000782 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000783
halcanary96fcdcc2015-08-27 07:41:13 -0700784 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000785 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000786 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000787 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000789 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000790 element->asPath(&clipPath);
791 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000792 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000793 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000794 }
robertphillips68737822015-10-29 12:12:21 -0700795
796 SkASSERT(pipelineBuilder.getStencil().isDisabled());
797
798 GrPathRenderer::CanDrawPathArgs canDrawArgs;
799 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
800 canDrawArgs.fViewMatrix = &viewMatrix;
801 canDrawArgs.fPath = &clipPath;
802 canDrawArgs.fStroke = &stroke;
803 canDrawArgs.fAntiAlias = false;
804 canDrawArgs.fIsStencilDisabled = pipelineBuilder.getStencil().isDisabled();
805 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
806
807 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
808 GrPathRendererChain::kStencilOnly_DrawType,
809 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700810 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 return false;
812 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 }
814
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 int passes;
816 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
817
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000818 bool canRenderDirectToStencil =
819 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000820 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000821 // fill rule, and set operation can
822 // we render the element directly to
823 // stencil bit used for clipping.
824 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
825 canRenderDirectToStencil,
826 clipBit,
827 fillInverted,
828 &passes,
829 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830
831 // draw the element to the client stencil bits if necessary
832 if (!canDrawDirectToClip) {
833 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000834 kIncClamp_StencilOp,
835 kIncClamp_StencilOp,
836 kAlways_StencilFunc,
837 0xffff,
838 0x0000,
839 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000840 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800841 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700842
joshualitta8b84992016-01-13 13:35:35 -0800843 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
844 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000845 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000846 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000847 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800848 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700849
850 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700851 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700852 args.fResourceProvider = this->getContext()->resourceProvider();
853 args.fPipelineBuilder = &pipelineBuilder;
854 args.fColor = GrColor_WHITE;
855 args.fViewMatrix = &viewMatrix;
856 args.fPath = &clipPath;
857 args.fStroke = &stroke;
858 args.fAntiAlias = false;
859 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000860 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700861 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700862 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700863 args.fResourceProvider = this->getContext()->resourceProvider();
864 args.fPipelineBuilder = &pipelineBuilder;
865 args.fViewMatrix = &viewMatrix;
866 args.fPath = &clipPath;
867 args.fStroke = &stroke;
868 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000869 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 }
871 }
872 }
873
874 // now we modify the clip bit by rendering either the clip
875 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700876 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000877 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700878 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800879
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000881 if (Element::kRect_Type == element->getType()) {
joshualitta8b84992016-01-13 13:35:35 -0800882 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
883 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700885 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700886 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700887 args.fResourceProvider = this->getContext()->resourceProvider();
888 args.fPipelineBuilder = &pipelineBuilder;
889 args.fColor = GrColor_WHITE;
890 args.fViewMatrix = &viewMatrix;
891 args.fPath = &clipPath;
892 args.fStroke = &stroke;
893 args.fAntiAlias = false;
894 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 }
896 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000897 // The view matrix is setup to do clip space -> stencil space translation, so
898 // draw rect in clip space.
joshualitta8b84992016-01-13 13:35:35 -0800899 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
900 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000901 }
902 }
903 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000904 }
joshualitt7a6184f2014-10-29 18:29:27 -0700905 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000906 return true;
907}
908
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909// mapping of clip-respecting stencil funcs to normal stencil funcs
910// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000911static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000912 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
913 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
914 // In the Clip Funcs
915 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
916 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
917 kLess_StencilFunc, // kLessIfInClip_StencilFunc
918 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
919 // Special in the clip func that forces user's ref to be 0.
920 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
921 // make ref 0 and do normal nequal.
922 },
923 {// Stencil-Clipping is ENABLED
924 // In the Clip Funcs
925 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
926 // eq stencil clip bit, mask
927 // out user bits.
928
929 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
930 // add stencil bit to mask and ref
931
932 kLess_StencilFunc, // kLessIfInClip_StencilFunc
933 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
934 // for both of these we can add
935 // the clip bit to the mask and
936 // ref and compare as normal
937 // Special in the clip func that forces user's ref to be 0.
938 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
939 // make ref have only the clip bit set
940 // and make comparison be less
941 // 10..0 < 1..user_bits..
942 }
943};
944
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945namespace {
946// Sets the settings to clip against the stencil buffer clip while ignoring the
947// client bits.
948const GrStencilSettings& basic_apply_stencil_clip_settings() {
949 // stencil settings to use when clip is in stencil
950 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
951 kKeep_StencilOp,
952 kKeep_StencilOp,
953 kAlwaysIfInClip_StencilFunc,
954 0x0000,
955 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000956 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
958}
959}
960
joshualitt5e6ba212015-07-13 07:35:05 -0700961void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800962 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000963 // We make two copies of the StencilSettings here (except in the early
964 // exit scenario. One copy from draw state to the stack var. Then another
965 // from the stack var to the gpu. We could make this class hold a ptr to
966 // GrGpu's fStencilSettings and eliminate the stack copy here.
967
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 // use stencil for clipping if clipping is enabled and the clip
969 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800971
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 // The GrGpu client may not be using the stencil buffer but we may need to
973 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700974 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700975 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 settings = basic_apply_stencil_clip_settings();
977 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 return;
979 }
980 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700981 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 }
983
bsalomon@google.coma3201942012-06-21 19:58:20 +0000984 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700985 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
robertphillips544b9aa2015-10-28 11:01:41 -0700986 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700987 if (stencilAttachment) {
988 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000989 }
990
robertphillips544b9aa2015-10-28 11:01:41 -0700991 SkASSERT(this->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
992 SkASSERT(this->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700993 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700994 ars->set(&pipelineBuilder);
995 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000996}
997
998void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
999 StencilClipMode mode,
1000 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001001 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001002
1003 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 // We assume that this clip manager itself is drawing to the GrGpu and
1005 // has already setup the correct values.
1006 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001007 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001008
bsalomon@google.com411dad02012-06-05 20:24:20 +00001009 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1010 unsigned int userBits = clipBit - 1;
1011
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
robertphillips544b9aa2015-10-28 11:01:41 -07001013 bool twoSided = this->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014
bsalomon@google.coma3201942012-06-21 19:58:20 +00001015 bool finished = false;
1016 while (!finished) {
1017 GrStencilFunc func = settings->func(face);
1018 uint16_t writeMask = settings->writeMask(face);
1019 uint16_t funcMask = settings->funcMask(face);
1020 uint16_t funcRef = settings->funcRef(face);
1021
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001022 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001023
1024 writeMask &= userBits;
1025
1026 if (func >= kBasicStencilFuncCount) {
1027 int respectClip = kRespectClip_StencilClipMode == mode;
1028 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029 switch (func) {
1030 case kAlwaysIfInClip_StencilFunc:
1031 funcMask = clipBit;
1032 funcRef = clipBit;
1033 break;
1034 case kEqualIfInClip_StencilFunc:
1035 case kLessIfInClip_StencilFunc:
1036 case kLEqualIfInClip_StencilFunc:
1037 funcMask = (funcMask & userBits) | clipBit;
1038 funcRef = (funcRef & userBits) | clipBit;
1039 break;
1040 case kNonZeroIfInClip_StencilFunc:
1041 funcMask = (funcMask & userBits) | clipBit;
1042 funcRef = clipBit;
1043 break;
1044 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001045 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001046 }
1047 } else {
1048 funcMask &= userBits;
1049 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001050 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001051 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001052 gSpecialToBasicStencilFunc[respectClip];
1053 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001054 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001055 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001056 funcMask &= userBits;
1057 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001058 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001059
1060 settings->setFunc(face, func);
1061 settings->setWriteMask(face, writeMask);
1062 settings->setFuncMask(face, funcMask);
1063 settings->setFuncRef(face, funcRef);
1064
1065 if (GrStencilSettings::kFront_Face == face) {
1066 face = GrStencilSettings::kBack_Face;
1067 finished = !twoSided;
1068 } else {
1069 finished = true;
1070 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001071 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001072 if (!twoSided) {
1073 settings->copyFrontSettingsToBack();
1074 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001075}
1076
1077////////////////////////////////////////////////////////////////////////////////
robertphillips391395d2016-03-02 09:26:36 -08001078GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context,
1079 int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001080 GrReducedClip::InitialState initialState,
1081 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001082 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001083 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001084 GrUniqueKey key;
1085 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips391395d2016-03-02 09:26:36 -08001086 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -07001087 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1088 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001089 }
1090
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001091 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1092 // the top left corner of the resulting rect to the top left of the texture.
1093 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1094
robertphillips391395d2016-03-02 09:26:36 -08001095 GrSWMaskHelper helper(context);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001096
joshualitt8059eb92014-12-29 15:10:07 -08001097 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1098 // space.
1099 SkMatrix translate;
1100 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001101
joshualitt8059eb92014-12-29 15:10:07 -08001102 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001103 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001104 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001105
tfarinabf54e492014-10-23 17:47:18 -07001106 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001107 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001108 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001109
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001110 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1111 // Intersect and reverse difference require modifying pixels outside of the geometry
1112 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1113 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1114 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001115 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001116 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001117 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001118 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001119 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001120 SkPath clipPath;
1121 element->asPath(&clipPath);
1122 clipPath.toggleInverseFillType();
1123 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001124 continue;
1125 }
1126
1127 // The other ops (union, xor, diff) only affect pixels inside
1128 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001129 if (Element::kRect_Type == element->getType()) {
1130 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1131 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001132 SkPath path;
1133 element->asPath(&path);
1134 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001135 }
1136 }
1137
krajcevskiad1dc582014-06-10 15:06:47 -07001138 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -08001139 GrSurfaceDesc desc;
1140 desc.fWidth = clipSpaceIBounds.width();
1141 desc.fHeight = clipSpaceIBounds.height();
1142 desc.fConfig = kAlpha_8_GrPixelConfig;
1143
1144 GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0);
1145 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001146 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001147 }
robertphillips391395d2016-03-02 09:26:36 -08001148 result->resourcePriv().setUniqueKey(key);
1149
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001150 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001151
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001152 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001153}
1154
robertphillips@google.comf294b772012-04-27 14:29:26 +00001155////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001156
egdaniel8dc7c3a2015-04-16 11:22:42 -07001157void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001158 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001159 if (stencilAttachment) {
1160 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001161 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001162 }
1163}