blob: 4b464bb01469101d781811d9a3a4f1391d54db91 [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
bsalomon0ba8c242015-10-07 09:20:28 -070049static bool path_needs_SW_renderer(GrContext* context,
50 const GrDrawTarget* gpu,
51 const GrPipelineBuilder& pipelineBuilder,
52 const SkMatrix& viewMatrix,
53 const SkPath& origPath,
54 const GrStrokeInfo& stroke,
55 bool doAA) {
robertphillips@google.come79f3202014-02-11 16:30:21 +000056 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
57 SkTCopyOnFirstWrite<SkPath> path(origPath);
58 if (path->isInverseFillType()) {
59 path.writable()->toggleInverseFillType();
60 }
61 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000062 GrPathRendererChain::DrawType type = doAA ?
63 GrPathRendererChain::kColorAntiAlias_DrawType :
64 GrPathRendererChain::kColor_DrawType;
65
halcanary96fcdcc2015-08-27 07:41:13 -070066 return nullptr == context->getPathRenderer(gpu, &pipelineBuilder, viewMatrix, *path, stroke,
egdaniel8dd688b2015-01-22 10:16:09 -080067 false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +000068}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000069
bsalomonb3b9aec2015-09-10 11:16:35 -070070GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -070071 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -070072 , fClipMode(kIgnoreClip_StencilClipMode) {
73}
74
bsalomonb3b9aec2015-09-10 11:16:35 -070075GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -070076
robertphillips@google.comfa662942012-05-17 12:20:22 +000077/*
78 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
79 * will be used on any element. If so, it returns true to indicate that the
80 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
81 */
joshualitt5e6ba212015-07-13 07:35:05 -070082bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080083 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -080084 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000085 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000086 // a clip gets complex enough it can just be done in SW regardless
87 // of whether it would invoke the GrSoftwarePathRenderer.
kkinnunen18996512015-04-26 23:18:49 -070088 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000089
joshualitt8059eb92014-12-29 15:10:07 -080090 // Set the matrix so that rendered clip elements are transformed to mask space from clip
91 // space.
92 SkMatrix translate;
93 translate.setTranslate(clipToMaskOffset);
94
tfarinabf54e492014-10-23 17:47:18 -070095 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000097 // rects can always be drawn directly w/o using the software path
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000098 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
100 SkPath path;
101 element->asPath(&path);
bsalomonb3b9aec2015-09-10 11:16:35 -0700102 if (path_needs_SW_renderer(this->getContext(), fDrawTarget, pipelineBuilder, translate,
joshualitt8059eb92014-12-29 15:10:07 -0800103 path, stroke, element->isAA())) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000104 return true;
105 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000109}
110
bsalomon0b5b6b22015-10-14 08:31:34 -0700111bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
112 SkVector& clipToRTOffset,
113 const SkRect* drawBounds,
114 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000115 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700116 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000117 boundsInClipSpace = *drawBounds;
118 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
119 }
bsalomon0ba8c242015-10-07 09:20:28 -0700120 SkASSERT(elements.count() <= kMaxAnalyticElements);
121 const GrFragmentProcessor* fps[kMaxAnalyticElements];
122 for (int i = 0; i < kMaxAnalyticElements; ++i) {
123 fps[i] = nullptr;
124 }
125 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700126 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000127 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700128 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000129 SkRegion::Op op = iter.get()->getOp();
130 bool invert;
131 bool skip = false;
132 switch (op) {
133 case SkRegion::kReplace_Op:
134 SkASSERT(iter.get() == elements.head());
135 // Fallthrough, handled same as intersect.
136 case SkRegion::kIntersect_Op:
137 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700138 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000139 skip = true;
140 }
141 break;
142 case SkRegion::kDifference_Op:
143 invert = true;
144 // We don't currently have a cheap test for whether a rect is fully outside an
145 // element's primitive, so don't attempt to set skip.
146 break;
147 default:
148 failed = true;
149 break;
150 }
151 if (failed) {
152 break;
153 }
154
155 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700156 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800157 if (iter.get()->isAA()) {
joshualittb0a8a372014-09-23 09:50:21 -0700158 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700159 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700161 edgeType =
162 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000164 switch (iter.get()->getType()) {
165 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700166 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
167 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000168 break;
169 case SkClipStack::Element::kRRect_Type: {
170 SkRRect rrect = iter.get()->getRRect();
171 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700172 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000173 break;
174 }
175 case SkClipStack::Element::kRect_Type: {
176 SkRect rect = iter.get()->getRect();
177 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700178 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 break;
180 }
181 default:
182 break;
183 }
bsalomon0ba8c242015-10-07 09:20:28 -0700184 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000185 failed = true;
186 break;
187 }
bsalomon0ba8c242015-10-07 09:20:28 -0700188 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 }
mtklein217daa72014-07-02 12:55:21 -0700190 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 }
192
bsalomon0b5b6b22015-10-14 08:31:34 -0700193 *resultFP = nullptr;
194 if (!failed && fpCnt) {
195 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 }
bsalomon0ba8c242015-10-07 09:20:28 -0700197 for (int i = 0; i < fpCnt; ++i) {
198 fps[i]->unref();
199 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700200 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201}
202
robertphillips@google.comf294b772012-04-27 14:29:26 +0000203////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000204// sort out what kind of clip mask needs to be created: alpha, stencil,
205// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700206bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800207 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800208 GrScissorState* scissorState,
bsalomon0ba8c242015-10-07 09:20:28 -0700209 const SkRect* devBounds,
210 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700211 if (kRespectClip_StencilClipMode == fClipMode) {
212 fClipMode = kIgnoreClip_StencilClipMode;
213 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000214
tfarinabf54e492014-10-23 17:47:18 -0700215 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800216 int32_t genID = 0;
217 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800219 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700220 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700223 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224
joshualitt44701df2015-02-23 14:44:57 -0800225 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700226 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800227 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800228 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229 return true;
230 }
231
bsalomon96e02a82015-03-06 07:13:01 -0800232 // The clip mask manager always draws with a single IRect so we special case that logic here
233 // Image filters just use a rect, so we also special case that logic
234 switch (clip.clipType()) {
235 case GrClip::kWideOpen_ClipType:
236 SkFAIL("Should have caught this with clip.isWideOpen()");
237 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800238 case GrClip::kIRect_ClipType: {
239 SkIRect scissor = clip.irect();
240 if (scissor.intersect(clipSpaceRTIBounds)) {
241 scissorState->set(scissor);
242 this->setPipelineBuilderStencil(pipelineBuilder, ars);
243 return true;
244 }
245 return false;
246 }
bsalomon96e02a82015-03-06 07:13:01 -0800247 case GrClip::kClipStack_ClipType: {
248 clipSpaceRTIBounds.offset(clip.origin());
249 GrReducedClip::ReduceClipStack(*clip.clipStack(),
250 clipSpaceRTIBounds,
251 &elements,
252 &genID,
253 &initialState,
254 &clipSpaceIBounds,
255 &requiresAA);
256 if (elements.isEmpty()) {
257 if (GrReducedClip::kAllIn_InitialState == initialState) {
258 if (clipSpaceIBounds == clipSpaceRTIBounds) {
259 this->setPipelineBuilderStencil(pipelineBuilder, ars);
260 return true;
261 }
262 } else {
263 return false;
264 }
265 }
266 } break;
267 }
268
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000269 // An element count of 4 was chosen because of the common pattern in Blink of:
270 // isect RR
271 // diff RR
272 // isect convex_poly
273 // isect convex_poly
274 // when drawing rounded div borders. This could probably be tuned based on a
275 // configuration's relative costs of switching RTs to generate a mask vs
276 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700277 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800278 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
279 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700280 // When there are multiple color samples we want to do per-sample clipping, not compute
281 // a fractional pixel coverage.
282 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
283 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000284 if (elements.isEmpty() ||
bsalomon0ba8c242015-10-07 09:20:28 -0700285 (requiresAA && !disallowAnalyticAA &&
bsalomon0b5b6b22015-10-14 08:31:34 -0700286 this->getAnalyticClipProcessor(elements, clipToRTOffset, devBounds, &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700287 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800288 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700289 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700290 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700291 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000292 }
egdaniel8dd688b2015-01-22 10:16:09 -0800293 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700294 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000295 return true;
296 }
297 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000298
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000299 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700300 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700301 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302
joshualitt8059eb92014-12-29 15:10:07 -0800303 // The top-left of the mask corresponds to the top-left corner of the bounds.
304 SkVector clipToMaskOffset = {
305 SkIntToScalar(-clipSpaceIBounds.fLeft),
306 SkIntToScalar(-clipSpaceIBounds.fTop)
307 };
308
egdaniel8dd688b2015-01-22 10:16:09 -0800309 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000310 // The clip geometry is complex enough that it will be more efficient to create it
311 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700312 result.reset(this->createSoftwareClipMask(genID,
313 initialState,
314 elements,
315 clipToMaskOffset,
316 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700318 result.reset(this->createAlphaClipMask(genID,
319 initialState,
320 elements,
321 clipToMaskOffset,
322 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000323 }
324
bsalomon49f085d2014-09-05 13:34:00 -0700325 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 // The mask's top left coord should be pinned to the rounded-out top left corner of
327 // clipSpace bounds. We determine the mask's position WRT to the render target here.
328 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800329 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700330 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800331 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332 return true;
333 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000337 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800338 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800339 this->createStencilClipMask(rt,
340 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000341 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000342 elements,
343 clipSpaceIBounds,
344 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
347 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
348 // use both stencil and scissor test to the bounds for the final draw.
349 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
350 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700351 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800352 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000353 return true;
354}
355
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800358// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
359void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
360 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800361 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800362 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000364}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
366////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800367bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800368 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800369 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000370 const SkClipStack::Element* element,
371 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372
egdaniel8dd688b2015-01-22 10:16:09 -0800373 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374
egdaniel87509242014-12-17 13:37:13 -0800375 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
376 // which ignores color.
377 GrColor color = GrColor_WHITE;
378
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000379 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000380 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000381 case Element::kEmpty_Type:
382 SkDEBUGFAIL("Should never get here with an empty element.");
383 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000384 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700385 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
386 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000387 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800388 SkRect devRect = element->getRect();
389 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700390
bsalomonb3b9aec2015-09-10 11:16:35 -0700391 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700392 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000393 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700394 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700395 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000396 }
397 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000398 default: {
399 SkPath path;
400 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700401 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000402 if (path.isInverseFillType()) {
403 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000404 }
kkinnunen18996512015-04-26 23:18:49 -0700405 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700406 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000407 GrPathRendererChain::DrawType type;
408 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
409 GrPathRendererChain::kColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700410 pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800411 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000412 }
halcanary96fcdcc2015-08-27 07:41:13 -0700413 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000414 return false;
415 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700416 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700417 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700418 args.fResourceProvider = this->getContext()->resourceProvider();
419 args.fPipelineBuilder = pipelineBuilder;
420 args.fColor = color;
421 args.fViewMatrix = &viewMatrix;
422 args.fPath = &path;
423 args.fStroke = &stroke;
424 args.fAntiAlias = element->isAA();
425 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000426 break;
427 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428 }
429 return true;
430}
431
egdaniel8dd688b2015-01-22 10:16:09 -0800432bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800433 GrTexture* target,
434 GrPathRenderer** pr,
435 const SkClipStack::Element* element) {
egdaniel8dd688b2015-01-22 10:16:09 -0800436 pipelineBuilder->setRenderTarget(target->asRenderTarget());
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000437
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000438 if (Element::kRect_Type == element->getType()) {
439 return true;
440 } else {
441 // We shouldn't get here with an empty clip element.
442 SkASSERT(Element::kEmpty_Type != element->getType());
443 SkPath path;
444 element->asPath(&path);
445 if (path.isInverseFillType()) {
446 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000447 }
kkinnunen18996512015-04-26 23:18:49 -0700448 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000449 GrPathRendererChain::DrawType type = element->isAA() ?
450 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
451 GrPathRendererChain::kStencilAndColor_DrawType;
bsalomonb3b9aec2015-09-10 11:16:35 -0700452 *pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, SkMatrix::I(), path,
joshualitt8059eb92014-12-29 15:10:07 -0800453 stroke, false, type);
bsalomon49f085d2014-09-05 13:34:00 -0700454 return SkToBool(*pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000455 }
456}
457
egdaniel8dd688b2015-01-22 10:16:09 -0800458void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800459 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000460 GrTexture* srcMask,
461 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000462 const SkIRect& dstBound,
463 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800464 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000465
egdaniel87509242014-12-17 13:37:13 -0800466 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800467 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000468
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000469 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000470 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000471
bsalomonac856c92015-08-27 06:30:17 -0700472 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700473 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000474 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000475 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
476 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000477 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700478
egdaniel87509242014-12-17 13:37:13 -0800479 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700480 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700481 GrColor_WHITE,
482 SkMatrix::I(),
483 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484}
485
bsalomon427cf282014-10-16 13:41:43 -0700486GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700487 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800488 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 desc.fWidth = width;
490 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700491 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800492 desc.fConfig = kAlpha_8_GrPixelConfig;
493 } else {
494 desc.fConfig = kRGBA_8888_GrPixelConfig;
495 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496
bsalomoneae62002015-07-31 13:59:30 -0700497 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000498}
499
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000500////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700501// Create a 8-bit clip mask in alpha
502
503static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
504 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
505 GrUniqueKey::Builder builder(key, kDomain, 3);
506 builder[0] = clipGenID;
507 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
508 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
509}
510
511GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
512 bool renderTarget) {
513 GrSurfaceDesc desc;
514 desc.fWidth = width;
515 desc.fHeight = height;
516 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
517 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
518 desc.fConfig = kAlpha_8_GrPixelConfig;
519 } else {
520 desc.fConfig = kRGBA_8888_GrPixelConfig;
521 }
522
523 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
524 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700525 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000526 }
bsalomon473addf2015-10-02 07:49:05 -0700527 texture->resourcePriv().setUniqueKey(key);
528 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700529}
530
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000531GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700532 GrReducedClip::InitialState initialState,
533 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800534 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000535 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700536 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
537 GrUniqueKey key;
538 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
539 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700540 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000541 }
542
bsalomon473addf2015-10-02 07:49:05 -0700543 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
544 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
545
krajcevskiad1dc582014-06-10 15:06:47 -0700546 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700547 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700548 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549 }
550
joshualitt8059eb92014-12-29 15:10:07 -0800551 // Set the matrix so that rendered clip elements are transformed to mask space from clip
552 // space.
553 SkMatrix translate;
554 translate.setTranslate(clipToMaskOffset);
555
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000556 // The texture may be larger than necessary, this rect represents the part of the texture
557 // we populate with a rasterization of the clip.
558 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
559
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000560 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
561 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700562 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700563 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
564 true,
bsalomon473addf2015-10-02 07:49:05 -0700565 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000566
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000567 // When we use the stencil in the below loop it is important to have this clip installed.
568 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
569 // pass must not set values outside of this bounds or stencil values outside the rect won't be
570 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800571 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700572 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800573
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700575 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000577 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000578 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000579 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800580 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800581
joshualitt44701df2015-02-23 14:44:57 -0800582 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700583 GrPathRenderer* pr = nullptr;
bsalomon473addf2015-10-02 07:49:05 -0700584 bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000585 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000586 // 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 +0000587 // mask buffer can be substantially larger than the actually clip stack element. We
588 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000589 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000590 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000591
592 if (useTemp) {
593 if (invert) {
594 maskSpaceElementIBounds = maskSpaceIBounds;
595 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000596 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000597 elementBounds.offset(clipToMaskOffset);
598 elementBounds.roundOut(&maskSpaceElementIBounds);
599 }
600
bsalomon427cf282014-10-16 13:41:43 -0700601 if (!temp) {
602 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
603 maskSpaceIBounds.fBottom));
604 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700605 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700606 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700607 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000608 }
bsalomon427cf282014-10-16 13:41:43 -0700609 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000610 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700611 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800612 invert ? 0xffffffff : 0x00000000,
613 true,
614 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800615 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617 // draw directly into the result with the stencil set to make the pixels affected
618 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700619 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
621 kReplace_StencilOp,
622 kReplace_StencilOp,
623 kAlways_StencilFunc,
624 0xffff,
625 0xffff,
626 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800627 pipelineBuilder.setStencil(kStencilInElement);
628 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000629 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000630
egdaniel8dd688b2015-01-22 10:16:09 -0800631 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700632 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700633 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000634 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000636 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800637 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700638 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800639
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000640 // Now draw into the accumulator using the real operation and the temp buffer as a
641 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800642 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700643 texture,
bsalomon427cf282014-10-16 13:41:43 -0700644 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000645 op,
646 maskSpaceIBounds,
647 maskSpaceElementIBounds);
648 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800649 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700650 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800651
egdaniel8dd688b2015-01-22 10:16:09 -0800652 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000653 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000654 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
655 kZero_StencilOp,
656 kZero_StencilOp,
657 kEqual_StencilFunc,
658 0xffff,
659 0x0000,
660 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800661 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700662
egdaniel87509242014-12-17 13:37:13 -0800663 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700664 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700665 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000666 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800668 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800669
robertphillips@google.come79f3202014-02-11 16:30:21 +0000670 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800671 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800672 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700673 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674 }
675 }
676
bsalomon473addf2015-10-02 07:49:05 -0700677 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000678}
679
680////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000681// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000682// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800683bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
684 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700685 GrReducedClip::InitialState initialState,
686 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000687 const SkIRect& clipSpaceIBounds,
688 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700689 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690
egdanielec00d942015-09-14 12:56:10 -0700691 GrStencilAttachment* stencilAttachment =
692 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700693 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 return false;
695 }
696
egdaniel8dc7c3a2015-04-16 11:22:42 -0700697 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
698 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000699 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
700 SkVector translate = {
701 SkIntToScalar(clipSpaceToStencilOffset.fX),
702 SkIntToScalar(clipSpaceToStencilOffset.fY)
703 };
joshualitt8059eb92014-12-29 15:10:07 -0800704 SkMatrix viewMatrix;
705 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
bsalomon@google.com9f131742012-12-13 20:43:56 +0000707 // We set the current clip to the bounds so that our recursive draws are scissored to them.
708 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
709 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800710 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
egdaniel8dc7c3a2015-04-16 11:22:42 -0700712 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000713 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 clipBit = (1 << (clipBit-1));
715
bsalomonb3b9aec2015-09-10 11:16:35 -0700716 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
717 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718
719 // walk through each clip element and perform its set op
720 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700721 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000722 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800723
egdaniel8dd688b2015-01-22 10:16:09 -0800724 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800725 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800726 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800727
egdaniel8dd688b2015-01-22 10:16:09 -0800728 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800729
730 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700731 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700732 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800733 }
734
tomhudson@google.com8afae612012-08-14 15:03:35 +0000735 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700737 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000739 // This will be used to determine whether the clip shape can be rendered into the
740 // stencil with arbitrary stencil settings.
741 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742
kkinnunen18996512015-04-26 23:18:49 -0700743 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000744 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000745
halcanary96fcdcc2015-08-27 07:41:13 -0700746 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000747 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000748 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000749 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000751 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000752 element->asPath(&clipPath);
753 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000754 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000755 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000756 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700757 pr = this->getContext()->getPathRenderer(fDrawTarget,
egdaniel8dd688b2015-01-22 10:16:09 -0800758 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800759 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800760 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000761 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000762 false,
763 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000764 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700765 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000766 return false;
767 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 }
769
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 int passes;
771 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
772
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000773 bool canRenderDirectToStencil =
774 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000776 // fill rule, and set operation can
777 // we render the element directly to
778 // stencil bit used for clipping.
779 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
780 canRenderDirectToStencil,
781 clipBit,
782 fillInverted,
783 &passes,
784 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785
786 // draw the element to the client stencil bits if necessary
787 if (!canDrawDirectToClip) {
788 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000789 kIncClamp_StencilOp,
790 kIncClamp_StencilOp,
791 kAlways_StencilFunc,
792 0xffff,
793 0x0000,
794 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000795 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800796 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700797
798 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700799 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700800 GrColor_WHITE,
801 viewMatrix,
802 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000804 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000805 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800806 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700807
808 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700809 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700810 args.fResourceProvider = this->getContext()->resourceProvider();
811 args.fPipelineBuilder = &pipelineBuilder;
812 args.fColor = GrColor_WHITE;
813 args.fViewMatrix = &viewMatrix;
814 args.fPath = &clipPath;
815 args.fStroke = &stroke;
816 args.fAntiAlias = false;
817 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000818 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700819 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700820 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700821 args.fResourceProvider = this->getContext()->resourceProvider();
822 args.fPipelineBuilder = &pipelineBuilder;
823 args.fViewMatrix = &viewMatrix;
824 args.fPath = &clipPath;
825 args.fStroke = &stroke;
826 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000827 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 }
829 }
830 }
831
832 // now we modify the clip bit by rendering either the clip
833 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700834 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700836 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800837
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000839 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700840 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700841 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700842 GrColor_WHITE,
843 viewMatrix,
844 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000845 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700846 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700847 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700848 args.fResourceProvider = this->getContext()->resourceProvider();
849 args.fPipelineBuilder = &pipelineBuilder;
850 args.fColor = GrColor_WHITE;
851 args.fViewMatrix = &viewMatrix;
852 args.fPath = &clipPath;
853 args.fStroke = &stroke;
854 args.fAntiAlias = false;
855 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
857 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000858 // The view matrix is setup to do clip space -> stencil space translation, so
859 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700860 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700861 GrColor_WHITE,
862 viewMatrix,
863 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 }
865 }
866 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000867 }
joshualitt7a6184f2014-10-29 18:29:27 -0700868 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 return true;
870}
871
bsalomon@google.com411dad02012-06-05 20:24:20 +0000872// mapping of clip-respecting stencil funcs to normal stencil funcs
873// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000874static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000875 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
876 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
877 // In the Clip Funcs
878 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
879 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
880 kLess_StencilFunc, // kLessIfInClip_StencilFunc
881 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
882 // Special in the clip func that forces user's ref to be 0.
883 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
884 // make ref 0 and do normal nequal.
885 },
886 {// Stencil-Clipping is ENABLED
887 // In the Clip Funcs
888 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
889 // eq stencil clip bit, mask
890 // out user bits.
891
892 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
893 // add stencil bit to mask and ref
894
895 kLess_StencilFunc, // kLessIfInClip_StencilFunc
896 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
897 // for both of these we can add
898 // the clip bit to the mask and
899 // ref and compare as normal
900 // Special in the clip func that forces user's ref to be 0.
901 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
902 // make ref have only the clip bit set
903 // and make comparison be less
904 // 10..0 < 1..user_bits..
905 }
906};
907
bsalomon@google.coma3201942012-06-21 19:58:20 +0000908namespace {
909// Sets the settings to clip against the stencil buffer clip while ignoring the
910// client bits.
911const GrStencilSettings& basic_apply_stencil_clip_settings() {
912 // stencil settings to use when clip is in stencil
913 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
914 kKeep_StencilOp,
915 kKeep_StencilOp,
916 kAlwaysIfInClip_StencilFunc,
917 0x0000,
918 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000919 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
921}
922}
923
joshualitt5e6ba212015-07-13 07:35:05 -0700924void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800925 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000926 // We make two copies of the StencilSettings here (except in the early
927 // exit scenario. One copy from draw state to the stack var. Then another
928 // from the stack var to the gpu. We could make this class hold a ptr to
929 // GrGpu's fStencilSettings and eliminate the stack copy here.
930
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 // use stencil for clipping if clipping is enabled and the clip
932 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800934
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935 // The GrGpu client may not be using the stencil buffer but we may need to
936 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700937 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700938 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939 settings = basic_apply_stencil_clip_settings();
940 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000941 return;
942 }
943 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700944 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 }
946
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700948 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700949 GrStencilAttachment* stencilAttachment =
950 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700951 if (stencilAttachment) {
952 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000953 }
954
bsalomonb3b9aec2015-09-10 11:16:35 -0700955 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
956 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700957 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700958 ars->set(&pipelineBuilder);
959 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960}
961
962void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
963 StencilClipMode mode,
964 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000965 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000966
967 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 // We assume that this clip manager itself is drawing to the GrGpu and
969 // has already setup the correct values.
970 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000971 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972
bsalomon@google.com411dad02012-06-05 20:24:20 +0000973 unsigned int clipBit = (1 << (stencilBitCnt - 1));
974 unsigned int userBits = clipBit - 1;
975
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -0700977 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978
bsalomon@google.coma3201942012-06-21 19:58:20 +0000979 bool finished = false;
980 while (!finished) {
981 GrStencilFunc func = settings->func(face);
982 uint16_t writeMask = settings->writeMask(face);
983 uint16_t funcMask = settings->funcMask(face);
984 uint16_t funcRef = settings->funcRef(face);
985
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000986 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000987
988 writeMask &= userBits;
989
990 if (func >= kBasicStencilFuncCount) {
991 int respectClip = kRespectClip_StencilClipMode == mode;
992 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993 switch (func) {
994 case kAlwaysIfInClip_StencilFunc:
995 funcMask = clipBit;
996 funcRef = clipBit;
997 break;
998 case kEqualIfInClip_StencilFunc:
999 case kLessIfInClip_StencilFunc:
1000 case kLEqualIfInClip_StencilFunc:
1001 funcMask = (funcMask & userBits) | clipBit;
1002 funcRef = (funcRef & userBits) | clipBit;
1003 break;
1004 case kNonZeroIfInClip_StencilFunc:
1005 funcMask = (funcMask & userBits) | clipBit;
1006 funcRef = clipBit;
1007 break;
1008 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001009 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001010 }
1011 } else {
1012 funcMask &= userBits;
1013 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001015 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 gSpecialToBasicStencilFunc[respectClip];
1017 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001018 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 funcMask &= userBits;
1021 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001023
1024 settings->setFunc(face, func);
1025 settings->setWriteMask(face, writeMask);
1026 settings->setFuncMask(face, funcMask);
1027 settings->setFuncRef(face, funcRef);
1028
1029 if (GrStencilSettings::kFront_Face == face) {
1030 face = GrStencilSettings::kBack_Face;
1031 finished = !twoSided;
1032 } else {
1033 finished = true;
1034 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001035 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001036 if (!twoSided) {
1037 settings->copyFrontSettingsToBack();
1038 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039}
1040
1041////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001042GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001043 GrReducedClip::InitialState initialState,
1044 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001045 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001046 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001047 GrUniqueKey key;
1048 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1049 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1050 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1051 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001052 }
1053
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001054 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1055 // the top left corner of the resulting rect to the top left of the texture.
1056 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1057
robertphillips@google.com2c756812012-05-22 20:28:23 +00001058 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001059
joshualitt8059eb92014-12-29 15:10:07 -08001060 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1061 // space.
1062 SkMatrix translate;
1063 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001064
joshualitt8059eb92014-12-29 15:10:07 -08001065 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001066 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001067 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001068
tfarinabf54e492014-10-23 17:47:18 -07001069 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001070 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001071 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001072
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001073 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1074 // Intersect and reverse difference require modifying pixels outside of the geometry
1075 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1076 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1077 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001078 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001079 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001080 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001081 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001082 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001083 SkPath clipPath;
1084 element->asPath(&clipPath);
1085 clipPath.toggleInverseFillType();
1086 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001087 continue;
1088 }
1089
1090 // The other ops (union, xor, diff) only affect pixels inside
1091 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001092 if (Element::kRect_Type == element->getType()) {
1093 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1094 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001095 SkPath path;
1096 element->asPath(&path);
1097 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001098 }
1099 }
1100
krajcevskiad1dc582014-06-10 15:06:47 -07001101 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001102 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1103 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001104 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001105 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001106 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001107 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001108
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001109 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001110}
1111
robertphillips@google.comf294b772012-04-27 14:29:26 +00001112////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001113
egdaniel8dc7c3a2015-04-16 11:22:42 -07001114void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001115 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001116 if (stencilAttachment) {
1117 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001118 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001119 }
1120}