blob: b87788b286bc0d22f32cd79c78430d53919e3524 [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
robertphillips5c3ea4c2015-10-26 08:33:10 -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,
robertphillips5c3ea4c2015-10-26 08:33:10 -070055 const Element* element,
56 GrPathRenderer** prOut,
57 bool needsStencil) {
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 return false;
62 } else {
63 // We shouldn't get here with an empty clip element.
64 SkASSERT(Element::kEmpty_Type != element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +000065
robertphillips5c3ea4c2015-10-26 08:33:10 -070066 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
67 SkPath path;
68 element->asPath(&path);
69 if (path.isInverseFillType()) {
70 path.toggleInverseFillType();
71 }
72 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
73
74 GrPathRendererChain::DrawType type;
75
76 if (needsStencil) {
77 type = element->isAA()
78 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
79 : GrPathRendererChain::kStencilAndColor_DrawType;
80 } else {
81 type = element->isAA()
82 ? GrPathRendererChain::kColorAntiAlias_DrawType
83 : GrPathRendererChain::kColor_DrawType;
84 }
85
86 // the 'false' parameter disallows use of the SW path renderer
87 GrPathRenderer* pr = context->getPathRenderer(pipelineBuilder, viewMatrix, path,
88 stroke, false, type);
89 if (prOut) {
90 *prOut = pr;
91 }
92 return SkToBool(!pr);
93 }
94}
95
96// Determines whether it is possible to draw the element to both the stencil buffer and the
97// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
98// also returned.
99static bool can_stencil_and_draw_element(GrContext* context,
100 GrPipelineBuilder* pipelineBuilder,
101 GrTexture* texture,
102 const SkMatrix& viewMatrix,
103 const SkClipStack::Element* element,
104 GrPathRenderer** pr) {
105 pipelineBuilder->setRenderTarget(texture->asRenderTarget());
106
107 static const bool kNeedsStencil = true;
108 return !path_needs_SW_renderer(context,
109 *pipelineBuilder,
110 viewMatrix,
111 element,
112 pr,
113 kNeedsStencil);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000114}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000115
bsalomonb3b9aec2015-09-10 11:16:35 -0700116GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -0700117 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -0700118 , fClipMode(kIgnoreClip_StencilClipMode) {
119}
120
bsalomonb3b9aec2015-09-10 11:16:35 -0700121GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -0700122
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123/*
124 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
125 * will be used on any element. If so, it returns true to indicate that the
126 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
127 */
joshualitt5e6ba212015-07-13 07:35:05 -0700128bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800129 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800130 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000131 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000132 // a clip gets complex enough it can just be done in SW regardless
133 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000134
joshualitt8059eb92014-12-29 15:10:07 -0800135 // Set the matrix so that rendered clip elements are transformed to mask space from clip
136 // space.
robertphillips5c3ea4c2015-10-26 08:33:10 -0700137 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800138
tfarinabf54e492014-10-23 17:47:18 -0700139 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000140 const Element* element = iter.get();
robertphillips5c3ea4c2015-10-26 08:33:10 -0700141
142 SkRegion::Op op = element->getOp();
143 bool invert = element->isInverseFilled();
144 bool needsStencil = invert ||
145 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
146
147 if (path_needs_SW_renderer(this->getContext(), pipelineBuilder, translate,
148 element, nullptr, needsStencil)) {
149 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000150 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000152 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000153}
154
bsalomon0b5b6b22015-10-14 08:31:34 -0700155bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700156 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700157 SkVector& clipToRTOffset,
158 const SkRect* drawBounds,
159 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700161 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000162 boundsInClipSpace = *drawBounds;
163 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
164 }
bsalomon0ba8c242015-10-07 09:20:28 -0700165 SkASSERT(elements.count() <= kMaxAnalyticElements);
166 const GrFragmentProcessor* fps[kMaxAnalyticElements];
167 for (int i = 0; i < kMaxAnalyticElements; ++i) {
168 fps[i] = nullptr;
169 }
170 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700171 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000172 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700173 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000174 SkRegion::Op op = iter.get()->getOp();
175 bool invert;
176 bool skip = false;
177 switch (op) {
178 case SkRegion::kReplace_Op:
179 SkASSERT(iter.get() == elements.head());
180 // Fallthrough, handled same as intersect.
181 case SkRegion::kIntersect_Op:
182 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700183 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 skip = true;
185 }
186 break;
187 case SkRegion::kDifference_Op:
188 invert = true;
189 // We don't currently have a cheap test for whether a rect is fully outside an
190 // element's primitive, so don't attempt to set skip.
191 break;
192 default:
193 failed = true;
194 break;
195 }
196 if (failed) {
197 break;
198 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000199 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700200 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800201 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700202 if (abortIfAA) {
203 failed = true;
204 break;
205 }
joshualittb0a8a372014-09-23 09:50:21 -0700206 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700207 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700209 edgeType =
210 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 }
bsalomona912dde2015-10-14 15:01:50 -0700212
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 switch (iter.get()->getType()) {
214 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700215 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
216 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 break;
218 case SkClipStack::Element::kRRect_Type: {
219 SkRRect rrect = iter.get()->getRRect();
220 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700221 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 break;
223 }
224 case SkClipStack::Element::kRect_Type: {
225 SkRect rect = iter.get()->getRect();
226 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700227 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 break;
229 }
230 default:
231 break;
232 }
bsalomon0ba8c242015-10-07 09:20:28 -0700233 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 failed = true;
235 break;
236 }
bsalomon0ba8c242015-10-07 09:20:28 -0700237 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 }
mtklein217daa72014-07-02 12:55:21 -0700239 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
241
bsalomon0b5b6b22015-10-14 08:31:34 -0700242 *resultFP = nullptr;
243 if (!failed && fpCnt) {
244 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000245 }
bsalomon0ba8c242015-10-07 09:20:28 -0700246 for (int i = 0; i < fpCnt; ++i) {
247 fps[i]->unref();
248 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700249 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000250}
251
robertphillips@google.comf294b772012-04-27 14:29:26 +0000252////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000253// sort out what kind of clip mask needs to be created: alpha, stencil,
254// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700255bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800256 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon3e791242014-12-17 13:43:13 -0800257 GrScissorState* scissorState,
bsalomon0ba8c242015-10-07 09:20:28 -0700258 const SkRect* devBounds,
259 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700260 if (kRespectClip_StencilClipMode == fClipMode) {
261 fClipMode = kIgnoreClip_StencilClipMode;
262 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000263
tfarinabf54e492014-10-23 17:47:18 -0700264 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800265 int32_t genID = 0;
266 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000267 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800268 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700269 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000270
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000271 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700272 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273
joshualitt44701df2015-02-23 14:44:57 -0800274 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700275 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800276 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800277 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278 return true;
279 }
280
bsalomon96e02a82015-03-06 07:13:01 -0800281 // The clip mask manager always draws with a single IRect so we special case that logic here
282 // Image filters just use a rect, so we also special case that logic
283 switch (clip.clipType()) {
284 case GrClip::kWideOpen_ClipType:
285 SkFAIL("Should have caught this with clip.isWideOpen()");
286 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800287 case GrClip::kIRect_ClipType: {
288 SkIRect scissor = clip.irect();
289 if (scissor.intersect(clipSpaceRTIBounds)) {
290 scissorState->set(scissor);
291 this->setPipelineBuilderStencil(pipelineBuilder, ars);
292 return true;
293 }
294 return false;
295 }
bsalomon96e02a82015-03-06 07:13:01 -0800296 case GrClip::kClipStack_ClipType: {
297 clipSpaceRTIBounds.offset(clip.origin());
298 GrReducedClip::ReduceClipStack(*clip.clipStack(),
299 clipSpaceRTIBounds,
300 &elements,
301 &genID,
302 &initialState,
303 &clipSpaceIBounds,
304 &requiresAA);
305 if (elements.isEmpty()) {
306 if (GrReducedClip::kAllIn_InitialState == initialState) {
307 if (clipSpaceIBounds == clipSpaceRTIBounds) {
308 this->setPipelineBuilderStencil(pipelineBuilder, ars);
309 return true;
310 }
311 } else {
312 return false;
313 }
314 }
315 } break;
316 }
317
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000318 // An element count of 4 was chosen because of the common pattern in Blink of:
319 // isect RR
320 // diff RR
321 // isect convex_poly
322 // isect convex_poly
323 // when drawing rounded div borders. This could probably be tuned based on a
324 // configuration's relative costs of switching RTs to generate a mask vs
325 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700326 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800327 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
328 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700329 // When there are multiple color samples we want to do per-sample clipping, not compute
330 // a fractional pixel coverage.
331 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
332 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000333 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700334 (requiresAA &&
335 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
336 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700337 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800338 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700339 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700340 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
joshualitt77b13072014-10-27 14:51:01 -0700341 scissorState->set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000342 }
egdaniel8dd688b2015-01-22 10:16:09 -0800343 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700344 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000345 return true;
346 }
347 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000348
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000349 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700350 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700351 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352
joshualitt8059eb92014-12-29 15:10:07 -0800353 // The top-left of the mask corresponds to the top-left corner of the bounds.
354 SkVector clipToMaskOffset = {
355 SkIntToScalar(-clipSpaceIBounds.fLeft),
356 SkIntToScalar(-clipSpaceIBounds.fTop)
357 };
358
egdaniel8dd688b2015-01-22 10:16:09 -0800359 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000360 // The clip geometry is complex enough that it will be more efficient to create it
361 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700362 result.reset(this->createSoftwareClipMask(genID,
363 initialState,
364 elements,
365 clipToMaskOffset,
366 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700368 result.reset(this->createAlphaClipMask(genID,
369 initialState,
370 elements,
371 clipToMaskOffset,
372 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000373 }
374
bsalomon49f085d2014-09-05 13:34:00 -0700375 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000376 // The mask's top left coord should be pinned to the rounded-out top left corner of
377 // clipSpace bounds. We determine the mask's position WRT to the render target here.
378 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800379 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700380 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800381 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 return true;
383 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000384 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000387 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800388 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800389 this->createStencilClipMask(rt,
390 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000391 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000392 elements,
393 clipSpaceIBounds,
394 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000395
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
397 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
398 // use both stencil and scissor test to the bounds for the final draw.
399 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
400 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
joshualitt77b13072014-10-27 14:51:01 -0700401 scissorState->set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800402 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000403 return true;
404}
405
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000406namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000407////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800408// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
409void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
410 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800411 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800412 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000414}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
416////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800417bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800418 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800419 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000420 const SkClipStack::Element* element,
421 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000422
egdaniel8dd688b2015-01-22 10:16:09 -0800423 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000424
egdaniel87509242014-12-17 13:37:13 -0800425 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
426 // which ignores color.
427 GrColor color = GrColor_WHITE;
428
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000429 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000430 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000431 case Element::kEmpty_Type:
432 SkDEBUGFAIL("Should never get here with an empty element.");
433 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000434 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700435 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
436 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000437 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800438 SkRect devRect = element->getRect();
439 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700440
bsalomonb3b9aec2015-09-10 11:16:35 -0700441 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700442 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000443 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700444 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700445 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000446 }
447 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000448 default: {
449 SkPath path;
450 element->asPath(&path);
jvanverthb3eb6872014-10-24 07:12:51 -0700451 path.setIsVolatile(true);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000452 if (path.isInverseFillType()) {
453 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000454 }
kkinnunen18996512015-04-26 23:18:49 -0700455 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700456 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000457 GrPathRendererChain::DrawType type;
458 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
459 GrPathRendererChain::kColor_DrawType;
robertphillips5c3ea4c2015-10-26 08:33:10 -0700460 pr = this->getContext()->getPathRenderer(*pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800461 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000462 }
halcanary96fcdcc2015-08-27 07:41:13 -0700463 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 return false;
465 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700466 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700467 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700468 args.fResourceProvider = this->getContext()->resourceProvider();
469 args.fPipelineBuilder = pipelineBuilder;
470 args.fColor = color;
471 args.fViewMatrix = &viewMatrix;
472 args.fPath = &path;
473 args.fStroke = &stroke;
474 args.fAntiAlias = element->isAA();
475 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 break;
477 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000478 }
479 return true;
480}
481
egdaniel8dd688b2015-01-22 10:16:09 -0800482void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800483 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000484 GrTexture* srcMask,
485 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000486 const SkIRect& dstBound,
487 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800488 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000489
egdaniel87509242014-12-17 13:37:13 -0800490 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800491 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000492
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000493 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000494 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000495
bsalomonac856c92015-08-27 06:30:17 -0700496 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700497 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000498 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000499 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
500 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000501 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700502
egdaniel87509242014-12-17 13:37:13 -0800503 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700504 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700505 GrColor_WHITE,
506 SkMatrix::I(),
507 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000508}
509
bsalomon427cf282014-10-16 13:41:43 -0700510GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700511 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800512 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000513 desc.fWidth = width;
514 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700515 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800516 desc.fConfig = kAlpha_8_GrPixelConfig;
517 } else {
518 desc.fConfig = kRGBA_8888_GrPixelConfig;
519 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000520
bsalomoneae62002015-07-31 13:59:30 -0700521 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000522}
523
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000524////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700525// Create a 8-bit clip mask in alpha
526
527static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
528 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
529 GrUniqueKey::Builder builder(key, kDomain, 3);
530 builder[0] = clipGenID;
531 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
532 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
533}
534
535GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
536 bool renderTarget) {
537 GrSurfaceDesc desc;
538 desc.fWidth = width;
539 desc.fHeight = height;
540 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
541 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
542 desc.fConfig = kAlpha_8_GrPixelConfig;
543 } else {
544 desc.fConfig = kRGBA_8888_GrPixelConfig;
545 }
546
547 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
548 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700549 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000550 }
bsalomon473addf2015-10-02 07:49:05 -0700551 texture->resourcePriv().setUniqueKey(key);
552 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700553}
554
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000555GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700556 GrReducedClip::InitialState initialState,
557 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800558 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000559 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700560 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
561 GrUniqueKey key;
562 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
563 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700564 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000565 }
566
bsalomon473addf2015-10-02 07:49:05 -0700567 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
568 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
569
krajcevskiad1dc582014-06-10 15:06:47 -0700570 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700571 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700572 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000573 }
574
joshualitt8059eb92014-12-29 15:10:07 -0800575 // Set the matrix so that rendered clip elements are transformed to mask space from clip
576 // space.
robertphillips5c3ea4c2015-10-26 08:33:10 -0700577 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800578
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000579 // The texture may be larger than necessary, this rect represents the part of the texture
580 // we populate with a rasterization of the clip.
581 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
582
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000583 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
584 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700585 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700586 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
587 true,
bsalomon473addf2015-10-02 07:49:05 -0700588 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000589
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 // When we use the stencil in the below loop it is important to have this clip installed.
591 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
592 // pass must not set values outside of this bounds or stencil values outside the rect won't be
593 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800594 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700595 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800596
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700598 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000599 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000600 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000601 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000602 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800603 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800604
joshualitt44701df2015-02-23 14:44:57 -0800605 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700606 GrPathRenderer* pr = nullptr;
robertphillips5c3ea4c2015-10-26 08:33:10 -0700607 bool useTemp = !can_stencil_and_draw_element(this->getContext(), &pipelineBuilder,
608 texture, translate, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000609 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000610 // 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 +0000611 // mask buffer can be substantially larger than the actually clip stack element. We
612 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000613 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000614 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000615
robertphillips5c3ea4c2015-10-26 08:33:10 -0700616 SkASSERT(!useTemp);
617
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000618 if (useTemp) {
619 if (invert) {
620 maskSpaceElementIBounds = maskSpaceIBounds;
621 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000622 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623 elementBounds.offset(clipToMaskOffset);
624 elementBounds.roundOut(&maskSpaceElementIBounds);
625 }
626
bsalomon427cf282014-10-16 13:41:43 -0700627 if (!temp) {
628 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
629 maskSpaceIBounds.fBottom));
630 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700631 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700632 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700633 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000634 }
bsalomon427cf282014-10-16 13:41:43 -0700635 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000636 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700637 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800638 invert ? 0xffffffff : 0x00000000,
639 true,
640 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800641 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000642 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000643 // draw directly into the result with the stencil set to make the pixels affected
644 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700645 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000646 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
647 kReplace_StencilOp,
648 kReplace_StencilOp,
649 kAlways_StencilFunc,
650 0xffff,
651 0xffff,
652 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800653 pipelineBuilder.setStencil(kStencilInElement);
654 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000655 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000656
egdaniel8dd688b2015-01-22 10:16:09 -0800657 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700658 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700659 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000660 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000662 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800663 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700664 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800665
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000666 // Now draw into the accumulator using the real operation and the temp buffer as a
667 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800668 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700669 texture,
bsalomon427cf282014-10-16 13:41:43 -0700670 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000671 op,
672 maskSpaceIBounds,
673 maskSpaceElementIBounds);
674 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800675 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700676 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800677
egdaniel8dd688b2015-01-22 10:16:09 -0800678 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000679 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000680 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
681 kZero_StencilOp,
682 kZero_StencilOp,
683 kEqual_StencilFunc,
684 0xffff,
685 0x0000,
686 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800687 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700688
egdaniel87509242014-12-17 13:37:13 -0800689 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700690 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700691 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000692 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800694 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800695
robertphillips@google.come79f3202014-02-11 16:30:21 +0000696 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800697 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800698 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700699 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000700 }
701 }
702
bsalomon473addf2015-10-02 07:49:05 -0700703 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000704}
705
706////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000707// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000708// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800709bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
710 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700711 GrReducedClip::InitialState initialState,
712 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000713 const SkIRect& clipSpaceIBounds,
714 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700715 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716
egdanielec00d942015-09-14 12:56:10 -0700717 GrStencilAttachment* stencilAttachment =
718 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700719 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 return false;
721 }
722
egdaniel8dc7c3a2015-04-16 11:22:42 -0700723 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
724 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000725 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
726 SkVector translate = {
727 SkIntToScalar(clipSpaceToStencilOffset.fX),
728 SkIntToScalar(clipSpaceToStencilOffset.fY)
729 };
joshualitt8059eb92014-12-29 15:10:07 -0800730 SkMatrix viewMatrix;
731 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732
bsalomon@google.com9f131742012-12-13 20:43:56 +0000733 // We set the current clip to the bounds so that our recursive draws are scissored to them.
734 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
735 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800736 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
egdaniel8dc7c3a2015-04-16 11:22:42 -0700738 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000739 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 clipBit = (1 << (clipBit-1));
741
bsalomonb3b9aec2015-09-10 11:16:35 -0700742 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
743 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744
745 // walk through each clip element and perform its set op
746 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700747 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000748 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800749
egdaniel8dd688b2015-01-22 10:16:09 -0800750 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800751 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800752 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800753
egdaniel8dd688b2015-01-22 10:16:09 -0800754 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800755
756 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700757 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700758 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800759 }
760
tomhudson@google.com8afae612012-08-14 15:03:35 +0000761 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700763 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000765 // This will be used to determine whether the clip shape can be rendered into the
766 // stencil with arbitrary stencil settings.
767 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768
kkinnunen18996512015-04-26 23:18:49 -0700769 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000770 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000771
halcanary96fcdcc2015-08-27 07:41:13 -0700772 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000773 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000774 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000775 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000777 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000778 element->asPath(&clipPath);
779 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000780 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000781 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000782 }
robertphillips5c3ea4c2015-10-26 08:33:10 -0700783 pr = this->getContext()->getPathRenderer(pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800784 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800785 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000786 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000787 false,
788 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000789 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700790 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 return false;
792 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 }
794
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795 int passes;
796 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
797
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000798 bool canRenderDirectToStencil =
799 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000801 // fill rule, and set operation can
802 // we render the element directly to
803 // stencil bit used for clipping.
804 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
805 canRenderDirectToStencil,
806 clipBit,
807 fillInverted,
808 &passes,
809 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810
811 // draw the element to the client stencil bits if necessary
812 if (!canDrawDirectToClip) {
813 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000814 kIncClamp_StencilOp,
815 kIncClamp_StencilOp,
816 kAlways_StencilFunc,
817 0xffff,
818 0x0000,
819 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000820 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800821 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700822
823 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700824 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700825 GrColor_WHITE,
826 viewMatrix,
827 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000829 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000830 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800831 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700832
833 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700834 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700835 args.fResourceProvider = this->getContext()->resourceProvider();
836 args.fPipelineBuilder = &pipelineBuilder;
837 args.fColor = GrColor_WHITE;
838 args.fViewMatrix = &viewMatrix;
839 args.fPath = &clipPath;
840 args.fStroke = &stroke;
841 args.fAntiAlias = false;
842 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000843 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700844 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700845 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700846 args.fResourceProvider = this->getContext()->resourceProvider();
847 args.fPipelineBuilder = &pipelineBuilder;
848 args.fViewMatrix = &viewMatrix;
849 args.fPath = &clipPath;
850 args.fStroke = &stroke;
851 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000852 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000853 }
854 }
855 }
856
857 // now we modify the clip bit by rendering either the clip
858 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700859 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000860 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700861 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800862
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000863 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000864 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700865 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700866 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700867 GrColor_WHITE,
868 viewMatrix,
869 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700871 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700872 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700873 args.fResourceProvider = this->getContext()->resourceProvider();
874 args.fPipelineBuilder = &pipelineBuilder;
875 args.fColor = GrColor_WHITE;
876 args.fViewMatrix = &viewMatrix;
877 args.fPath = &clipPath;
878 args.fStroke = &stroke;
879 args.fAntiAlias = false;
880 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000881 }
882 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000883 // The view matrix is setup to do clip space -> stencil space translation, so
884 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700885 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700886 GrColor_WHITE,
887 viewMatrix,
888 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000889 }
890 }
891 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000892 }
joshualitt7a6184f2014-10-29 18:29:27 -0700893 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000894 return true;
895}
896
bsalomon@google.com411dad02012-06-05 20:24:20 +0000897// mapping of clip-respecting stencil funcs to normal stencil funcs
898// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000899static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000900 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
901 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
902 // In the Clip Funcs
903 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
904 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
905 kLess_StencilFunc, // kLessIfInClip_StencilFunc
906 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
907 // Special in the clip func that forces user's ref to be 0.
908 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
909 // make ref 0 and do normal nequal.
910 },
911 {// Stencil-Clipping is ENABLED
912 // In the Clip Funcs
913 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
914 // eq stencil clip bit, mask
915 // out user bits.
916
917 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
918 // add stencil bit to mask and ref
919
920 kLess_StencilFunc, // kLessIfInClip_StencilFunc
921 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
922 // for both of these we can add
923 // the clip bit to the mask and
924 // ref and compare as normal
925 // Special in the clip func that forces user's ref to be 0.
926 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
927 // make ref have only the clip bit set
928 // and make comparison be less
929 // 10..0 < 1..user_bits..
930 }
931};
932
bsalomon@google.coma3201942012-06-21 19:58:20 +0000933namespace {
934// Sets the settings to clip against the stencil buffer clip while ignoring the
935// client bits.
936const GrStencilSettings& basic_apply_stencil_clip_settings() {
937 // stencil settings to use when clip is in stencil
938 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
939 kKeep_StencilOp,
940 kKeep_StencilOp,
941 kAlwaysIfInClip_StencilFunc,
942 0x0000,
943 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000944 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000945 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
946}
947}
948
joshualitt5e6ba212015-07-13 07:35:05 -0700949void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800950 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 // We make two copies of the StencilSettings here (except in the early
952 // exit scenario. One copy from draw state to the stack var. Then another
953 // from the stack var to the gpu. We could make this class hold a ptr to
954 // GrGpu's fStencilSettings and eliminate the stack copy here.
955
bsalomon@google.coma3201942012-06-21 19:58:20 +0000956 // use stencil for clipping if clipping is enabled and the clip
957 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800959
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 // The GrGpu client may not be using the stencil buffer but we may need to
961 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700962 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700963 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 settings = basic_apply_stencil_clip_settings();
965 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 return;
967 }
968 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700969 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 }
971
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700973 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700974 GrStencilAttachment* stencilAttachment =
975 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700976 if (stencilAttachment) {
977 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 }
979
bsalomonb3b9aec2015-09-10 11:16:35 -0700980 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
981 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700982 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700983 ars->set(&pipelineBuilder);
984 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000985}
986
987void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
988 StencilClipMode mode,
989 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000990 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000991
992 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993 // We assume that this clip manager itself is drawing to the GrGpu and
994 // has already setup the correct values.
995 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000996 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000997
bsalomon@google.com411dad02012-06-05 20:24:20 +0000998 unsigned int clipBit = (1 << (stencilBitCnt - 1));
999 unsigned int userBits = clipBit - 1;
1000
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -07001002 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001003
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 bool finished = false;
1005 while (!finished) {
1006 GrStencilFunc func = settings->func(face);
1007 uint16_t writeMask = settings->writeMask(face);
1008 uint16_t funcMask = settings->funcMask(face);
1009 uint16_t funcRef = settings->funcRef(face);
1010
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001011 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012
1013 writeMask &= userBits;
1014
1015 if (func >= kBasicStencilFuncCount) {
1016 int respectClip = kRespectClip_StencilClipMode == mode;
1017 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018 switch (func) {
1019 case kAlwaysIfInClip_StencilFunc:
1020 funcMask = clipBit;
1021 funcRef = clipBit;
1022 break;
1023 case kEqualIfInClip_StencilFunc:
1024 case kLessIfInClip_StencilFunc:
1025 case kLEqualIfInClip_StencilFunc:
1026 funcMask = (funcMask & userBits) | clipBit;
1027 funcRef = (funcRef & userBits) | clipBit;
1028 break;
1029 case kNonZeroIfInClip_StencilFunc:
1030 funcMask = (funcMask & userBits) | clipBit;
1031 funcRef = clipBit;
1032 break;
1033 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001034 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001035 }
1036 } else {
1037 funcMask &= userBits;
1038 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001040 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001041 gSpecialToBasicStencilFunc[respectClip];
1042 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001043 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001044 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001045 funcMask &= userBits;
1046 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001047 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001048
1049 settings->setFunc(face, func);
1050 settings->setWriteMask(face, writeMask);
1051 settings->setFuncMask(face, funcMask);
1052 settings->setFuncRef(face, funcRef);
1053
1054 if (GrStencilSettings::kFront_Face == face) {
1055 face = GrStencilSettings::kBack_Face;
1056 finished = !twoSided;
1057 } else {
1058 finished = true;
1059 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001060 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001061 if (!twoSided) {
1062 settings->copyFrontSettingsToBack();
1063 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001064}
1065
1066////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001067GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001068 GrReducedClip::InitialState initialState,
1069 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001070 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001071 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001072 GrUniqueKey key;
1073 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1074 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1075 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1076 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001077 }
1078
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001079 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1080 // the top left corner of the resulting rect to the top left of the texture.
1081 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1082
robertphillips@google.com2c756812012-05-22 20:28:23 +00001083 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001084
joshualitt8059eb92014-12-29 15:10:07 -08001085 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1086 // space.
1087 SkMatrix translate;
1088 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001089
joshualitt8059eb92014-12-29 15:10:07 -08001090 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001091 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001092 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001093
tfarinabf54e492014-10-23 17:47:18 -07001094 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001095 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001096 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001097
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001098 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1099 // Intersect and reverse difference require modifying pixels outside of the geometry
1100 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1101 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1102 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001103 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001104 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001105 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001106 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001107 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001108 SkPath clipPath;
1109 element->asPath(&clipPath);
1110 clipPath.toggleInverseFillType();
1111 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001112 continue;
1113 }
1114
1115 // The other ops (union, xor, diff) only affect pixels inside
1116 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001117 if (Element::kRect_Type == element->getType()) {
1118 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1119 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001120 SkPath path;
1121 element->asPath(&path);
1122 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001123 }
1124 }
1125
krajcevskiad1dc582014-06-10 15:06:47 -07001126 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001127 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1128 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001129 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001130 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001131 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001132 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001133
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001134 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001135}
1136
robertphillips@google.comf294b772012-04-27 14:29:26 +00001137////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001138
egdaniel8dc7c3a2015-04-16 11:22:42 -07001139void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001140 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001141 if (stencilAttachment) {
1142 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001143 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001144 }
1145}