blob: 5c493aab7b3dc6943689122a6477f7599b06a08c [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"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
11#include "GrDrawTarget.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080022#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000031// stage matrix this also alters the vertex layout
bsalomon0ba8c242015-10-07 09:20:28 -070032static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000033 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080034 // We use device coords to compute the texture coordinates. We set our matrix to be a
35 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000038 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000040 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070041 return GrTextureDomainEffect::Create(result,
42 mat,
43 GrTextureDomain::MakeTexelDomain(result, domainTexels),
44 GrTextureDomain::kDecal_Mode,
45 GrTextureParams::kNone_FilterMode,
46 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
robertphillips3f7357f2015-10-27 07:17:33 -070049// Does the path in 'element' require SW rendering? If so, return true (and,
50// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
51// 'prOut' to the non-SW path renderer that will do the job).
bsalomon0ba8c242015-10-07 09:20:28 -070052static bool path_needs_SW_renderer(GrContext* context,
bsalomon0ba8c242015-10-07 09:20:28 -070053 const GrPipelineBuilder& pipelineBuilder,
54 const SkMatrix& viewMatrix,
robertphillips3f7357f2015-10-27 07:17:33 -070055 const Element* element,
56 GrPathRenderer** prOut,
57 bool tryStencilFirst) {
58 if (Element::kRect_Type == element->getType()) {
59 // rects can always be drawn directly w/o using the software path
60 // TODO: skip rrects once we're drawing them directly.
61 if (prOut) {
62 *prOut = nullptr;
63 }
64 return false;
65 } else {
66 // We shouldn't get here with an empty clip element.
67 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070068
robertphillips3f7357f2015-10-27 07:17:33 -070069 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
70 SkPath path;
71 element->asPath(&path);
72 if (path.isInverseFillType()) {
73 path.toggleInverseFillType();
74 }
75 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
76
77 GrPathRendererChain::DrawType type;
78
79 if (tryStencilFirst) {
80 type = element->isAA()
81 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
82 : GrPathRendererChain::kStencilAndColor_DrawType;
83 } else {
84 type = element->isAA()
85 ? GrPathRendererChain::kColorAntiAlias_DrawType
86 : GrPathRendererChain::kColor_DrawType;
87 }
88
89 // the 'false' parameter disallows use of the SW path renderer
90 GrPathRenderer* pr = context->getPathRenderer(&pipelineBuilder, viewMatrix, path,
91 stroke, false, type);
92 if (tryStencilFirst && !pr) {
93 // If the path can't be stenciled, createAlphaClipMask falls back to color rendering
94 // it into a temporary buffer. If that fails then SW is truly required.
95 type = element->isAA()
96 ? GrPathRendererChain::kColorAntiAlias_DrawType
97 : GrPathRendererChain::kColor_DrawType;
98
99 pr = context->getPathRenderer(&pipelineBuilder, viewMatrix, path, stroke, false, type);
100 }
101
102 if (prOut) {
103 *prOut = pr;
104 }
105 return SkToBool(!pr);
106 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000107}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000108
bsalomonb3b9aec2015-09-10 11:16:35 -0700109GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -0700110 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -0700111 , fClipMode(kIgnoreClip_StencilClipMode) {
112}
113
bsalomonb3b9aec2015-09-10 11:16:35 -0700114GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -0700115
robertphillips@google.comfa662942012-05-17 12:20:22 +0000116/*
117 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
118 * will be used on any element. If so, it returns true to indicate that the
119 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
120 */
joshualitt5e6ba212015-07-13 07:35:05 -0700121bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800122 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800123 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000124 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000125 // a clip gets complex enough it can just be done in SW regardless
126 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000127
joshualitt8059eb92014-12-29 15:10:07 -0800128 // Set the matrix so that rendered clip elements are transformed to mask space from clip
129 // space.
robertphillips24cdec12015-10-26 14:12:25 -0700130 SkMatrix translate;
131 translate.setTranslate(clipToMaskOffset);
joshualitt8059eb92014-12-29 15:10:07 -0800132
tfarinabf54e492014-10-23 17:47:18 -0700133 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000134 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700135
136 SkRegion::Op op = element->getOp();
137 bool invert = element->isInverseFilled();
138 bool tryStencilFirst = invert ||
139 SkRegion::kIntersect_Op == op ||
140 SkRegion::kReverseDifference_Op == op;
141
142 if (path_needs_SW_renderer(this->getContext(), pipelineBuilder, translate,
143 element, nullptr, tryStencilFirst)) {
144 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000145 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000146 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000147 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000148}
149
bsalomon0b5b6b22015-10-14 08:31:34 -0700150bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700151 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700152 SkVector& clipToRTOffset,
153 const SkRect* drawBounds,
154 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000155 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700156 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000157 boundsInClipSpace = *drawBounds;
158 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
159 }
bsalomon0ba8c242015-10-07 09:20:28 -0700160 SkASSERT(elements.count() <= kMaxAnalyticElements);
161 const GrFragmentProcessor* fps[kMaxAnalyticElements];
162 for (int i = 0; i < kMaxAnalyticElements; ++i) {
163 fps[i] = nullptr;
164 }
165 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700166 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700168 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 SkRegion::Op op = iter.get()->getOp();
170 bool invert;
171 bool skip = false;
172 switch (op) {
173 case SkRegion::kReplace_Op:
174 SkASSERT(iter.get() == elements.head());
175 // Fallthrough, handled same as intersect.
176 case SkRegion::kIntersect_Op:
177 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700178 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 skip = true;
180 }
181 break;
182 case SkRegion::kDifference_Op:
183 invert = true;
184 // We don't currently have a cheap test for whether a rect is fully outside an
185 // element's primitive, so don't attempt to set skip.
186 break;
187 default:
188 failed = true;
189 break;
190 }
191 if (failed) {
192 break;
193 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700195 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800196 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700197 if (abortIfAA) {
198 failed = true;
199 break;
200 }
joshualittb0a8a372014-09-23 09:50:21 -0700201 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700202 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700204 edgeType =
205 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 }
bsalomona912dde2015-10-14 15:01:50 -0700207
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 switch (iter.get()->getType()) {
209 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700210 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
211 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000212 break;
213 case SkClipStack::Element::kRRect_Type: {
214 SkRRect rrect = iter.get()->getRRect();
215 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700216 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 break;
218 }
219 case SkClipStack::Element::kRect_Type: {
220 SkRect rect = iter.get()->getRect();
221 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700222 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 break;
224 }
225 default:
226 break;
227 }
bsalomon0ba8c242015-10-07 09:20:28 -0700228 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000229 failed = true;
230 break;
231 }
bsalomon0ba8c242015-10-07 09:20:28 -0700232 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 }
mtklein217daa72014-07-02 12:55:21 -0700234 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235 }
236
bsalomon0b5b6b22015-10-14 08:31:34 -0700237 *resultFP = nullptr;
238 if (!failed && fpCnt) {
239 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
bsalomon0ba8c242015-10-07 09:20:28 -0700241 for (int i = 0; i < fpCnt; ++i) {
242 fps[i]->unref();
243 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700244 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000245}
246
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000248// sort out what kind of clip mask needs to be created: alpha, stencil,
249// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700250bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800251 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon0ba8c242015-10-07 09:20:28 -0700252 const SkRect* devBounds,
253 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700254 if (kRespectClip_StencilClipMode == fClipMode) {
255 fClipMode = kIgnoreClip_StencilClipMode;
256 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000257
tfarinabf54e492014-10-23 17:47:18 -0700258 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800259 int32_t genID = 0;
260 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000261 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800262 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700263 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000264
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000265 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700266 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000267
joshualitt44701df2015-02-23 14:44:57 -0800268 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700269 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800270 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800271 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272 return true;
273 }
274
bsalomon96e02a82015-03-06 07:13:01 -0800275 // The clip mask manager always draws with a single IRect so we special case that logic here
276 // Image filters just use a rect, so we also special case that logic
277 switch (clip.clipType()) {
278 case GrClip::kWideOpen_ClipType:
279 SkFAIL("Should have caught this with clip.isWideOpen()");
280 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800281 case GrClip::kIRect_ClipType: {
282 SkIRect scissor = clip.irect();
283 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700284 out->fScissorState.set(scissor);
bsalomon9ce30e12015-03-06 08:42:34 -0800285 this->setPipelineBuilderStencil(pipelineBuilder, ars);
286 return true;
287 }
288 return false;
289 }
bsalomon96e02a82015-03-06 07:13:01 -0800290 case GrClip::kClipStack_ClipType: {
291 clipSpaceRTIBounds.offset(clip.origin());
292 GrReducedClip::ReduceClipStack(*clip.clipStack(),
293 clipSpaceRTIBounds,
294 &elements,
295 &genID,
296 &initialState,
297 &clipSpaceIBounds,
298 &requiresAA);
299 if (elements.isEmpty()) {
300 if (GrReducedClip::kAllIn_InitialState == initialState) {
301 if (clipSpaceIBounds == clipSpaceRTIBounds) {
302 this->setPipelineBuilderStencil(pipelineBuilder, ars);
303 return true;
304 }
305 } else {
306 return false;
307 }
308 }
309 } break;
310 }
311
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000312 // An element count of 4 was chosen because of the common pattern in Blink of:
313 // isect RR
314 // diff RR
315 // isect convex_poly
316 // isect convex_poly
317 // when drawing rounded div borders. This could probably be tuned based on a
318 // configuration's relative costs of switching RTs to generate a mask vs
319 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700320 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800321 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
322 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700323 // When there are multiple color samples we want to do per-sample clipping, not compute
324 // a fractional pixel coverage.
325 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
326 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000327 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700328 (requiresAA &&
329 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
330 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700331 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800332 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700333 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700334 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700335 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000336 }
egdaniel8dd688b2015-01-22 10:16:09 -0800337 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700338 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000339 return true;
340 }
341 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000342
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000343 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700344 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700345 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346
joshualitt8059eb92014-12-29 15:10:07 -0800347 // The top-left of the mask corresponds to the top-left corner of the bounds.
348 SkVector clipToMaskOffset = {
349 SkIntToScalar(-clipSpaceIBounds.fLeft),
350 SkIntToScalar(-clipSpaceIBounds.fTop)
351 };
352
egdaniel8dd688b2015-01-22 10:16:09 -0800353 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 // The clip geometry is complex enough that it will be more efficient to create it
355 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700356 result.reset(this->createSoftwareClipMask(genID,
357 initialState,
358 elements,
359 clipToMaskOffset,
360 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000361 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700362 result.reset(this->createAlphaClipMask(genID,
363 initialState,
364 elements,
365 clipToMaskOffset,
366 clipSpaceIBounds));
robertphillips3f7357f2015-10-27 07:17:33 -0700367 // If createAlphaClipMask fails it means useSWOnlyPath has a bug
368 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000369 }
370
bsalomon49f085d2014-09-05 13:34:00 -0700371 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000372 // The mask's top left coord should be pinned to the rounded-out top left corner of
373 // clipSpace bounds. We determine the mask's position WRT to the render target here.
374 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800375 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700376 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800377 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378 return true;
379 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000380 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000383 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800384 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800385 this->createStencilClipMask(rt,
386 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000387 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388 elements,
389 clipSpaceIBounds,
390 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000391
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000392 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
393 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
394 // use both stencil and scissor test to the bounds for the final draw.
395 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
396 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700397 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800398 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000399 return true;
400}
401
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000402namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800404// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
405void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
406 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800407 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800408 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000409}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000410}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411
412////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800413bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800414 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800415 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000416 const SkClipStack::Element* element,
417 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000418
egdaniel8dd688b2015-01-22 10:16:09 -0800419 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420
egdaniel87509242014-12-17 13:37:13 -0800421 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
422 // which ignores color.
423 GrColor color = GrColor_WHITE;
424
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000425 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000426 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000427 case Element::kEmpty_Type:
428 SkDEBUGFAIL("Should never get here with an empty element.");
429 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000430 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700431 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
432 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000433 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800434 SkRect devRect = element->getRect();
435 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700436
bsalomonb3b9aec2015-09-10 11:16:35 -0700437 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700438 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000439 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700440 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700441 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000442 }
443 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000444 default: {
445 SkPath path;
446 element->asPath(&path);
447 if (path.isInverseFillType()) {
448 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000449 }
kkinnunen18996512015-04-26 23:18:49 -0700450 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700451 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000452 GrPathRendererChain::DrawType type;
453 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
454 GrPathRendererChain::kColor_DrawType;
robertphillips24cdec12015-10-26 14:12:25 -0700455 pr = this->getContext()->getPathRenderer(pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800456 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000457 }
halcanary96fcdcc2015-08-27 07:41:13 -0700458 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 return false;
460 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700461 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700462 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700463 args.fResourceProvider = this->getContext()->resourceProvider();
464 args.fPipelineBuilder = pipelineBuilder;
465 args.fColor = color;
466 args.fViewMatrix = &viewMatrix;
467 args.fPath = &path;
468 args.fStroke = &stroke;
469 args.fAntiAlias = element->isAA();
470 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000471 break;
472 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 }
474 return true;
475}
476
robertphillips24cdec12015-10-26 14:12:25 -0700477bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
478 GrTexture* target,
479 GrPathRenderer** pr,
480 const SkClipStack::Element* element) {
481 pipelineBuilder->setRenderTarget(target->asRenderTarget());
482
483 if (Element::kRect_Type == element->getType()) {
484 return true;
485 } else {
486 // We shouldn't get here with an empty clip element.
487 SkASSERT(Element::kEmpty_Type != element->getType());
488 SkPath path;
489 element->asPath(&path);
490 if (path.isInverseFillType()) {
491 path.toggleInverseFillType();
492 }
493 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
494 GrPathRendererChain::DrawType type = element->isAA() ?
495 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
496 GrPathRendererChain::kStencilAndColor_DrawType;
497 *pr = this->getContext()->getPathRenderer(pipelineBuilder, SkMatrix::I(), path,
498 stroke, false, type);
499 return SkToBool(*pr);
500 }
501}
502
egdaniel8dd688b2015-01-22 10:16:09 -0800503void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800504 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000505 GrTexture* srcMask,
506 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000507 const SkIRect& dstBound,
508 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800509 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000510
egdaniel87509242014-12-17 13:37:13 -0800511 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800512 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000513
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000514 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000515 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000516
bsalomonac856c92015-08-27 06:30:17 -0700517 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700518 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000519 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000520 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
521 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000522 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700523
egdaniel87509242014-12-17 13:37:13 -0800524 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700525 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700526 GrColor_WHITE,
527 SkMatrix::I(),
528 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000529}
530
bsalomon427cf282014-10-16 13:41:43 -0700531GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700532 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800533 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 desc.fWidth = width;
535 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700536 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800537 desc.fConfig = kAlpha_8_GrPixelConfig;
538 } else {
539 desc.fConfig = kRGBA_8888_GrPixelConfig;
540 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000541
bsalomoneae62002015-07-31 13:59:30 -0700542 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000543}
544
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000545////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700546// Create a 8-bit clip mask in alpha
547
548static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
549 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
550 GrUniqueKey::Builder builder(key, kDomain, 3);
551 builder[0] = clipGenID;
552 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
553 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
554}
555
556GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
557 bool renderTarget) {
558 GrSurfaceDesc desc;
559 desc.fWidth = width;
560 desc.fHeight = height;
561 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
562 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
563 desc.fConfig = kAlpha_8_GrPixelConfig;
564 } else {
565 desc.fConfig = kRGBA_8888_GrPixelConfig;
566 }
567
568 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
569 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700570 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 }
bsalomon473addf2015-10-02 07:49:05 -0700572 texture->resourcePriv().setUniqueKey(key);
573 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700574}
575
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000576GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700577 GrReducedClip::InitialState initialState,
578 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800579 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700581 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
582 GrUniqueKey key;
583 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
584 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700585 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586 }
587
bsalomon473addf2015-10-02 07:49:05 -0700588 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
589 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
590
krajcevskiad1dc582014-06-10 15:06:47 -0700591 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700592 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700593 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594 }
595
joshualitt8059eb92014-12-29 15:10:07 -0800596 // Set the matrix so that rendered clip elements are transformed to mask space from clip
597 // space.
robertphillips24cdec12015-10-26 14:12:25 -0700598 SkMatrix translate;
599 translate.setTranslate(clipToMaskOffset);
joshualitt8059eb92014-12-29 15:10:07 -0800600
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000601 // The texture may be larger than necessary, this rect represents the part of the texture
602 // we populate with a rasterization of the clip.
603 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
604
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000605 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
606 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700607 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700608 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
609 true,
bsalomon473addf2015-10-02 07:49:05 -0700610 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000611
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000612 // When we use the stencil in the below loop it is important to have this clip installed.
613 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
614 // pass must not set values outside of this bounds or stencil values outside the rect won't be
615 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800616 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700617 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800618
robertphillips@google.comf294b772012-04-27 14:29:26 +0000619 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700620 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000621 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000622 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000624 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800625 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800626
joshualitt44701df2015-02-23 14:44:57 -0800627 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700628 GrPathRenderer* pr = nullptr;
robertphillips24cdec12015-10-26 14:12:25 -0700629 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000630 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000631 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000632 // mask buffer can be substantially larger than the actually clip stack element. We
633 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000634 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000635 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000636
637 if (useTemp) {
638 if (invert) {
639 maskSpaceElementIBounds = maskSpaceIBounds;
640 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000641 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000642 elementBounds.offset(clipToMaskOffset);
643 elementBounds.roundOut(&maskSpaceElementIBounds);
644 }
645
bsalomon427cf282014-10-16 13:41:43 -0700646 if (!temp) {
647 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
648 maskSpaceIBounds.fBottom));
649 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700650 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700651 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700652 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000653 }
bsalomon427cf282014-10-16 13:41:43 -0700654 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000655 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700656 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800657 invert ? 0xffffffff : 0x00000000,
658 true,
659 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800660 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000661 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000662 // draw directly into the result with the stencil set to make the pixels affected
663 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700664 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000665 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
666 kReplace_StencilOp,
667 kReplace_StencilOp,
668 kAlways_StencilFunc,
669 0xffff,
670 0xffff,
671 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800672 pipelineBuilder.setStencil(kStencilInElement);
673 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000674 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000675
egdaniel8dd688b2015-01-22 10:16:09 -0800676 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700677 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700678 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000679 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000681 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800682 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700683 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800684
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000685 // Now draw into the accumulator using the real operation and the temp buffer as a
686 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800687 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700688 texture,
bsalomon427cf282014-10-16 13:41:43 -0700689 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000690 op,
691 maskSpaceIBounds,
692 maskSpaceElementIBounds);
693 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800694 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700695 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800696
egdaniel8dd688b2015-01-22 10:16:09 -0800697 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000698 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000699 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
700 kZero_StencilOp,
701 kZero_StencilOp,
702 kEqual_StencilFunc,
703 0xffff,
704 0x0000,
705 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800706 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700707
egdaniel87509242014-12-17 13:37:13 -0800708 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700709 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700710 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000711 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000712 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800713 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800714
robertphillips@google.come79f3202014-02-11 16:30:21 +0000715 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800716 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800717 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700718 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000719 }
720 }
721
bsalomon473addf2015-10-02 07:49:05 -0700722 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000723}
724
725////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000726// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000727// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800728bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
729 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700730 GrReducedClip::InitialState initialState,
731 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000732 const SkIRect& clipSpaceIBounds,
733 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700734 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735
egdanielec00d942015-09-14 12:56:10 -0700736 GrStencilAttachment* stencilAttachment =
737 fDrawTarget->cmmAccess().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
vbuzinov3e77ba92015-09-30 23:02:06 -0700776 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 }
robertphillips24cdec12015-10-26 14:12:25 -0700802 pr = this->getContext()->getPathRenderer(&pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800803 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800804 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000805 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000806 false,
807 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000808 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700809 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 return false;
811 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 }
813
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 int passes;
815 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
816
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000817 bool canRenderDirectToStencil =
818 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000820 // fill rule, and set operation can
821 // we render the element directly to
822 // stencil bit used for clipping.
823 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
824 canRenderDirectToStencil,
825 clipBit,
826 fillInverted,
827 &passes,
828 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829
830 // draw the element to the client stencil bits if necessary
831 if (!canDrawDirectToClip) {
832 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000833 kIncClamp_StencilOp,
834 kIncClamp_StencilOp,
835 kAlways_StencilFunc,
836 0xffff,
837 0x0000,
838 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000839 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800840 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700841
842 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700843 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700844 GrColor_WHITE,
845 viewMatrix,
846 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000848 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000849 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800850 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700851
852 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700853 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700854 args.fResourceProvider = this->getContext()->resourceProvider();
855 args.fPipelineBuilder = &pipelineBuilder;
856 args.fColor = GrColor_WHITE;
857 args.fViewMatrix = &viewMatrix;
858 args.fPath = &clipPath;
859 args.fStroke = &stroke;
860 args.fAntiAlias = false;
861 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000862 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700863 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700864 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700865 args.fResourceProvider = this->getContext()->resourceProvider();
866 args.fPipelineBuilder = &pipelineBuilder;
867 args.fViewMatrix = &viewMatrix;
868 args.fPath = &clipPath;
869 args.fStroke = &stroke;
870 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000871 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872 }
873 }
874 }
875
876 // now we modify the clip bit by rendering either the clip
877 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700878 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000879 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700880 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800881
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000882 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000883 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700884 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700885 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700886 GrColor_WHITE,
887 viewMatrix,
888 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000889 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700890 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700891 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700892 args.fResourceProvider = this->getContext()->resourceProvider();
893 args.fPipelineBuilder = &pipelineBuilder;
894 args.fColor = GrColor_WHITE;
895 args.fViewMatrix = &viewMatrix;
896 args.fPath = &clipPath;
897 args.fStroke = &stroke;
898 args.fAntiAlias = false;
899 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000900 }
901 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000902 // The view matrix is setup to do clip space -> stencil space translation, so
903 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700904 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700905 GrColor_WHITE,
906 viewMatrix,
907 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000908 }
909 }
910 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000911 }
joshualitt7a6184f2014-10-29 18:29:27 -0700912 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000913 return true;
914}
915
bsalomon@google.com411dad02012-06-05 20:24:20 +0000916// mapping of clip-respecting stencil funcs to normal stencil funcs
917// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000918static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000919 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
920 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
921 // In the Clip Funcs
922 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
923 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
924 kLess_StencilFunc, // kLessIfInClip_StencilFunc
925 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
926 // Special in the clip func that forces user's ref to be 0.
927 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
928 // make ref 0 and do normal nequal.
929 },
930 {// Stencil-Clipping is ENABLED
931 // In the Clip Funcs
932 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
933 // eq stencil clip bit, mask
934 // out user bits.
935
936 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
937 // add stencil bit to mask and ref
938
939 kLess_StencilFunc, // kLessIfInClip_StencilFunc
940 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
941 // for both of these we can add
942 // the clip bit to the mask and
943 // ref and compare as normal
944 // Special in the clip func that forces user's ref to be 0.
945 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
946 // make ref have only the clip bit set
947 // and make comparison be less
948 // 10..0 < 1..user_bits..
949 }
950};
951
bsalomon@google.coma3201942012-06-21 19:58:20 +0000952namespace {
953// Sets the settings to clip against the stencil buffer clip while ignoring the
954// client bits.
955const GrStencilSettings& basic_apply_stencil_clip_settings() {
956 // stencil settings to use when clip is in stencil
957 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
958 kKeep_StencilOp,
959 kKeep_StencilOp,
960 kAlwaysIfInClip_StencilFunc,
961 0x0000,
962 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000963 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
965}
966}
967
joshualitt5e6ba212015-07-13 07:35:05 -0700968void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800969 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 // We make two copies of the StencilSettings here (except in the early
971 // exit scenario. One copy from draw state to the stack var. Then another
972 // from the stack var to the gpu. We could make this class hold a ptr to
973 // GrGpu's fStencilSettings and eliminate the stack copy here.
974
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 // use stencil for clipping if clipping is enabled and the clip
976 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000977 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800978
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979 // The GrGpu client may not be using the stencil buffer but we may need to
980 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700981 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700982 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000983 settings = basic_apply_stencil_clip_settings();
984 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985 return;
986 }
987 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700988 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000989 }
990
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700992 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700993 GrStencilAttachment* stencilAttachment =
994 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700995 if (stencilAttachment) {
996 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000997 }
998
bsalomonb3b9aec2015-09-10 11:16:35 -0700999 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
1000 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -07001001 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -07001002 ars->set(&pipelineBuilder);
1003 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004}
1005
1006void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1007 StencilClipMode mode,
1008 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001009 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001010
1011 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 // We assume that this clip manager itself is drawing to the GrGpu and
1013 // has already setup the correct values.
1014 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1018 unsigned int userBits = clipBit - 1;
1019
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -07001021 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022
bsalomon@google.coma3201942012-06-21 19:58:20 +00001023 bool finished = false;
1024 while (!finished) {
1025 GrStencilFunc func = settings->func(face);
1026 uint16_t writeMask = settings->writeMask(face);
1027 uint16_t funcMask = settings->funcMask(face);
1028 uint16_t funcRef = settings->funcRef(face);
1029
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001030 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001031
1032 writeMask &= userBits;
1033
1034 if (func >= kBasicStencilFuncCount) {
1035 int respectClip = kRespectClip_StencilClipMode == mode;
1036 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001037 switch (func) {
1038 case kAlwaysIfInClip_StencilFunc:
1039 funcMask = clipBit;
1040 funcRef = clipBit;
1041 break;
1042 case kEqualIfInClip_StencilFunc:
1043 case kLessIfInClip_StencilFunc:
1044 case kLEqualIfInClip_StencilFunc:
1045 funcMask = (funcMask & userBits) | clipBit;
1046 funcRef = (funcRef & userBits) | clipBit;
1047 break;
1048 case kNonZeroIfInClip_StencilFunc:
1049 funcMask = (funcMask & userBits) | clipBit;
1050 funcRef = clipBit;
1051 break;
1052 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001053 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001054 }
1055 } else {
1056 funcMask &= userBits;
1057 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001058 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001059 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001060 gSpecialToBasicStencilFunc[respectClip];
1061 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001062 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001063 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001064 funcMask &= userBits;
1065 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001066 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001067
1068 settings->setFunc(face, func);
1069 settings->setWriteMask(face, writeMask);
1070 settings->setFuncMask(face, funcMask);
1071 settings->setFuncRef(face, funcRef);
1072
1073 if (GrStencilSettings::kFront_Face == face) {
1074 face = GrStencilSettings::kBack_Face;
1075 finished = !twoSided;
1076 } else {
1077 finished = true;
1078 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001079 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001080 if (!twoSided) {
1081 settings->copyFrontSettingsToBack();
1082 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001083}
1084
1085////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001086GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001087 GrReducedClip::InitialState initialState,
1088 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001089 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001090 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001091 GrUniqueKey key;
1092 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1093 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1094 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1095 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001096 }
1097
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001098 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1099 // the top left corner of the resulting rect to the top left of the texture.
1100 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1101
robertphillips@google.com2c756812012-05-22 20:28:23 +00001102 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001103
joshualitt8059eb92014-12-29 15:10:07 -08001104 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1105 // space.
1106 SkMatrix translate;
1107 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001108
joshualitt8059eb92014-12-29 15:10:07 -08001109 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001110 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001111 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001112
tfarinabf54e492014-10-23 17:47:18 -07001113 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001114 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001115 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001116
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001117 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1118 // Intersect and reverse difference require modifying pixels outside of the geometry
1119 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1120 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1121 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001123 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001124 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001125 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001126 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001127 SkPath clipPath;
1128 element->asPath(&clipPath);
1129 clipPath.toggleInverseFillType();
1130 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001131 continue;
1132 }
1133
1134 // The other ops (union, xor, diff) only affect pixels inside
1135 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001136 if (Element::kRect_Type == element->getType()) {
1137 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1138 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001139 SkPath path;
1140 element->asPath(&path);
1141 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001142 }
1143 }
1144
krajcevskiad1dc582014-06-10 15:06:47 -07001145 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001146 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1147 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001148 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001149 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001150 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001151 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001152
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001153 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001154}
1155
robertphillips@google.comf294b772012-04-27 14:29:26 +00001156////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001157
egdaniel8dc7c3a2015-04-16 11:22:42 -07001158void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001159 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001160 if (stencilAttachment) {
1161 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001162 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001163 }
1164}