blob: 9617e0dc659d16b4086c5afceff919ccb2f7b9bc [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillips68737822015-10-29 12:12:21 -070010#include "GrDrawingManager.h"
robertphillipsea461502015-05-26 11:38:03 -070011#include "GrDrawContext.h"
12#include "GrDrawTarget.h"
bsalomon473addf2015-10-02 07:49:05 -070013#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrPaint.h"
15#include "GrPathRenderer.h"
16#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070018#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070019#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070022#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080025#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080026#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
30////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
bsalomon0ba8c242015-10-07 09:20:28 -070033static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000034 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080035 // We use device coords to compute the texture coordinates. We set our matrix to be a
36 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000039 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000041 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070042 return GrTextureDomainEffect::Create(result,
43 mat,
44 GrTextureDomain::MakeTexelDomain(result, domainTexels),
45 GrTextureDomain::kDecal_Mode,
46 GrTextureParams::kNone_FilterMode,
47 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000048}
49
robertphillips3f7357f2015-10-27 07:17:33 -070050// Does the path in 'element' require SW rendering? If so, return true (and,
51// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
52// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070053bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
54 bool isStencilDisabled,
55 const GrRenderTarget* rt,
56 const SkMatrix& viewMatrix,
57 const Element* element,
58 GrPathRenderer** prOut,
59 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070060 if (Element::kRect_Type == element->getType()) {
61 // rects can always be drawn directly w/o using the software path
62 // TODO: skip rrects once we're drawing them directly.
63 if (prOut) {
64 *prOut = nullptr;
65 }
66 return false;
67 } else {
68 // We shouldn't get here with an empty clip element.
69 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070070
robertphillips3f7357f2015-10-27 07:17:33 -070071 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
72 SkPath path;
73 element->asPath(&path);
74 if (path.isInverseFillType()) {
75 path.toggleInverseFillType();
76 }
77 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
78
79 GrPathRendererChain::DrawType type;
80
robertphillips423e3372015-10-27 09:23:38 -070081 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070082 type = element->isAA()
83 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
84 : GrPathRendererChain::kStencilAndColor_DrawType;
85 } else {
86 type = element->isAA()
87 ? GrPathRendererChain::kColorAntiAlias_DrawType
88 : GrPathRendererChain::kColor_DrawType;
89 }
90
robertphillips68737822015-10-29 12:12:21 -070091 GrPathRenderer::CanDrawPathArgs canDrawArgs;
92 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
93 canDrawArgs.fViewMatrix = &viewMatrix;
94 canDrawArgs.fPath = &path;
95 canDrawArgs.fStroke = &stroke;
96 canDrawArgs.fAntiAlias = element->isAA();
97 canDrawArgs.fIsStencilDisabled = isStencilDisabled;
98 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
99
robertphillips3f7357f2015-10-27 07:17:33 -0700100 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700101 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700102 if (prOut) {
103 *prOut = pr;
104 }
105 return SkToBool(!pr);
106 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000107}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000108
robertphillips423e3372015-10-27 09:23:38 -0700109// Determines whether it is possible to draw the element to both the stencil buffer and the
110// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
111// also returned.
robertphillips68737822015-10-29 12:12:21 -0700112GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context,
113 GrTexture* texture,
114 const SkMatrix& viewMatrix,
115 const SkClipStack::Element* element) {
robertphillips544b9aa2015-10-28 11:01:41 -0700116 GrPathRenderer* pr;
robertphillips423e3372015-10-27 09:23:38 -0700117 static const bool kNeedsStencil = true;
robertphillips68737822015-10-29 12:12:21 -0700118 static const bool kStencilIsDisabled = true;
119 PathNeedsSWRenderer(context,
120 kStencilIsDisabled,
121 texture->asRenderTarget(),
122 viewMatrix,
123 element,
124 &pr,
125 kNeedsStencil);
robertphillips544b9aa2015-10-28 11:01:41 -0700126 return pr;
robertphillips423e3372015-10-27 09:23:38 -0700127}
128
bsalomonb3b9aec2015-09-10 11:16:35 -0700129GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -0700130 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -0700131 , fClipMode(kIgnoreClip_StencilClipMode) {
132}
133
robertphillips544b9aa2015-10-28 11:01:41 -0700134GrContext* GrClipMaskManager::getContext() {
135 return fDrawTarget->cmmAccess().context();
136}
bsalomonedd77a12015-05-29 09:45:57 -0700137
robertphillips544b9aa2015-10-28 11:01:41 -0700138const GrCaps* GrClipMaskManager::caps() const {
139 return fDrawTarget->caps();
140}
141
142GrResourceProvider* GrClipMaskManager::resourceProvider() {
143 return fDrawTarget->cmmAccess().resourceProvider();
144}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000145/*
146 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
147 * will be used on any element. If so, it returns true to indicate that the
148 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
149 */
joshualitt5e6ba212015-07-13 07:35:05 -0700150bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700151 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800152 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800153 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000154 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000155 // a clip gets complex enough it can just be done in SW regardless
156 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000157
joshualitt8059eb92014-12-29 15:10:07 -0800158 // Set the matrix so that rendered clip elements are transformed to mask space from clip
159 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700160 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800161
tfarinabf54e492014-10-23 17:47:18 -0700162 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000163 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700164
165 SkRegion::Op op = element->getOp();
166 bool invert = element->isInverseFilled();
robertphillips423e3372015-10-27 09:23:38 -0700167 bool needsStencil = invert ||
168 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700169
robertphillips68737822015-10-29 12:12:21 -0700170 if (PathNeedsSWRenderer(this->getContext(), pipelineBuilder.getStencil().isDisabled(),
171 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700172 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000173 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000174 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000175 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000176}
177
bsalomon0b5b6b22015-10-14 08:31:34 -0700178bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700179 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700180 SkVector& clipToRTOffset,
181 const SkRect* drawBounds,
182 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000183 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700184 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000185 boundsInClipSpace = *drawBounds;
186 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
187 }
bsalomon0ba8c242015-10-07 09:20:28 -0700188 SkASSERT(elements.count() <= kMaxAnalyticElements);
189 const GrFragmentProcessor* fps[kMaxAnalyticElements];
190 for (int i = 0; i < kMaxAnalyticElements; ++i) {
191 fps[i] = nullptr;
192 }
193 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700194 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700196 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 SkRegion::Op op = iter.get()->getOp();
198 bool invert;
199 bool skip = false;
200 switch (op) {
201 case SkRegion::kReplace_Op:
202 SkASSERT(iter.get() == elements.head());
203 // Fallthrough, handled same as intersect.
204 case SkRegion::kIntersect_Op:
205 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 skip = true;
208 }
209 break;
210 case SkRegion::kDifference_Op:
211 invert = true;
212 // We don't currently have a cheap test for whether a rect is fully outside an
213 // element's primitive, so don't attempt to set skip.
214 break;
215 default:
216 failed = true;
217 break;
218 }
219 if (failed) {
220 break;
221 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700223 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800224 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700225 if (abortIfAA) {
226 failed = true;
227 break;
228 }
joshualittb0a8a372014-09-23 09:50:21 -0700229 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700230 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700232 edgeType =
233 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 }
bsalomona912dde2015-10-14 15:01:50 -0700235
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000236 switch (iter.get()->getType()) {
237 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700238 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
239 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 break;
241 case SkClipStack::Element::kRRect_Type: {
242 SkRRect rrect = iter.get()->getRRect();
243 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700244 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000245 break;
246 }
247 case SkClipStack::Element::kRect_Type: {
248 SkRect rect = iter.get()->getRect();
249 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700250 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000251 break;
252 }
253 default:
254 break;
255 }
bsalomon0ba8c242015-10-07 09:20:28 -0700256 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 failed = true;
258 break;
259 }
bsalomon0ba8c242015-10-07 09:20:28 -0700260 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000261 }
mtklein217daa72014-07-02 12:55:21 -0700262 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263 }
264
bsalomon0b5b6b22015-10-14 08:31:34 -0700265 *resultFP = nullptr;
266 if (!failed && fpCnt) {
267 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000268 }
bsalomon0ba8c242015-10-07 09:20:28 -0700269 for (int i = 0; i < fpCnt; ++i) {
270 fps[i]->unref();
271 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700272 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000273}
274
robertphillips@google.comf294b772012-04-27 14:29:26 +0000275////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000276// sort out what kind of clip mask needs to be created: alpha, stencil,
277// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700278bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800279 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon0ba8c242015-10-07 09:20:28 -0700280 const SkRect* devBounds,
281 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700282 if (kRespectClip_StencilClipMode == fClipMode) {
283 fClipMode = kIgnoreClip_StencilClipMode;
284 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000285
tfarinabf54e492014-10-23 17:47:18 -0700286 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800287 int32_t genID = 0;
288 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000289 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800290 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700291 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000292
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700294 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000295
joshualitt44701df2015-02-23 14:44:57 -0800296 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700297 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800298 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800299 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 return true;
301 }
302
bsalomon96e02a82015-03-06 07:13:01 -0800303 // The clip mask manager always draws with a single IRect so we special case that logic here
304 // Image filters just use a rect, so we also special case that logic
305 switch (clip.clipType()) {
306 case GrClip::kWideOpen_ClipType:
307 SkFAIL("Should have caught this with clip.isWideOpen()");
308 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800309 case GrClip::kIRect_ClipType: {
310 SkIRect scissor = clip.irect();
311 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700312 out->fScissorState.set(scissor);
bsalomon9ce30e12015-03-06 08:42:34 -0800313 this->setPipelineBuilderStencil(pipelineBuilder, ars);
314 return true;
315 }
316 return false;
317 }
bsalomon96e02a82015-03-06 07:13:01 -0800318 case GrClip::kClipStack_ClipType: {
319 clipSpaceRTIBounds.offset(clip.origin());
320 GrReducedClip::ReduceClipStack(*clip.clipStack(),
321 clipSpaceRTIBounds,
322 &elements,
323 &genID,
324 &initialState,
325 &clipSpaceIBounds,
326 &requiresAA);
327 if (elements.isEmpty()) {
328 if (GrReducedClip::kAllIn_InitialState == initialState) {
329 if (clipSpaceIBounds == clipSpaceRTIBounds) {
330 this->setPipelineBuilderStencil(pipelineBuilder, ars);
331 return true;
332 }
333 } else {
334 return false;
335 }
336 }
337 } break;
338 }
339
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000340 // An element count of 4 was chosen because of the common pattern in Blink of:
341 // isect RR
342 // diff RR
343 // isect convex_poly
344 // isect convex_poly
345 // when drawing rounded div borders. This could probably be tuned based on a
346 // configuration's relative costs of switching RTs to generate a mask vs
347 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700348 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800349 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
350 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700351 // When there are multiple color samples we want to do per-sample clipping, not compute
352 // a fractional pixel coverage.
353 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
354 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000355 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700356 (requiresAA &&
357 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
358 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700359 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800360 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700361 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700362 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700363 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000364 }
egdaniel8dd688b2015-01-22 10:16:09 -0800365 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700366 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000367 return true;
368 }
369 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000370
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000371 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700372 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700373 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374
joshualitt8059eb92014-12-29 15:10:07 -0800375 // The top-left of the mask corresponds to the top-left corner of the bounds.
376 SkVector clipToMaskOffset = {
377 SkIntToScalar(-clipSpaceIBounds.fLeft),
378 SkIntToScalar(-clipSpaceIBounds.fTop)
379 };
380
robertphillips68737822015-10-29 12:12:21 -0700381 if (this->useSWOnlyPath(pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000382 // The clip geometry is complex enough that it will be more efficient to create it
383 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700384 result.reset(this->createSoftwareClipMask(genID,
385 initialState,
386 elements,
387 clipToMaskOffset,
388 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000389 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700390 result.reset(this->createAlphaClipMask(genID,
391 initialState,
392 elements,
393 clipToMaskOffset,
394 clipSpaceIBounds));
robertphillips3f7357f2015-10-27 07:17:33 -0700395 // If createAlphaClipMask fails it means useSWOnlyPath has a bug
396 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000397 }
398
bsalomon49f085d2014-09-05 13:34:00 -0700399 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 // The mask's top left coord should be pinned to the rounded-out top left corner of
401 // clipSpace bounds. We determine the mask's position WRT to the render target here.
402 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800403 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700404 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800405 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000406 return true;
407 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000409 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000411 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800412 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800413 this->createStencilClipMask(rt,
414 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000415 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416 elements,
417 clipSpaceIBounds,
418 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000419
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
421 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
422 // use both stencil and scissor test to the bounds for the final draw.
423 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
424 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700425 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800426 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000427 return true;
428}
429
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000430namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800432// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
433void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
434 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800435 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800436 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000438}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439
440////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800441bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800442 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800443 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000444 const SkClipStack::Element* element,
445 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446
robertphillips68737822015-10-29 12:12:21 -0700447 GrRenderTarget* rt = target->asRenderTarget();
448 pipelineBuilder->setRenderTarget(rt);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449
egdaniel87509242014-12-17 13:37:13 -0800450 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
451 // which ignores color.
452 GrColor color = GrColor_WHITE;
453
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000454 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000455 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000456 case Element::kEmpty_Type:
457 SkDEBUGFAIL("Should never get here with an empty element.");
458 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000459 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700460 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
461 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000462 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800463 SkRect devRect = element->getRect();
464 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700465
bsalomonb3b9aec2015-09-10 11:16:35 -0700466 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700467 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000468 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700469 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700470 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000471 }
472 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000473 default: {
474 SkPath path;
475 element->asPath(&path);
476 if (path.isInverseFillType()) {
477 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000478 }
kkinnunen18996512015-04-26 23:18:49 -0700479 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000481 GrPathRendererChain::DrawType type;
482 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
483 GrPathRendererChain::kColor_DrawType;
robertphillips68737822015-10-29 12:12:21 -0700484
485 GrPathRenderer::CanDrawPathArgs canDrawArgs;
486 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
487 canDrawArgs.fViewMatrix = &viewMatrix;
488 canDrawArgs.fPath = &path;
489 canDrawArgs.fStroke = &stroke;
490 canDrawArgs.fAntiAlias = element->isAA();;
491 canDrawArgs.fIsStencilDisabled = pipelineBuilder->getStencil().isDisabled();
492 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
493
494 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000495 }
halcanary96fcdcc2015-08-27 07:41:13 -0700496 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 return false;
498 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700499 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700500 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700501 args.fResourceProvider = this->getContext()->resourceProvider();
502 args.fPipelineBuilder = pipelineBuilder;
503 args.fColor = color;
504 args.fViewMatrix = &viewMatrix;
505 args.fPath = &path;
506 args.fStroke = &stroke;
507 args.fAntiAlias = element->isAA();
508 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 break;
510 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511 }
512 return true;
513}
514
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000515////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700516// Create a 8-bit clip mask in alpha
517
518static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
519 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
520 GrUniqueKey::Builder builder(key, kDomain, 3);
521 builder[0] = clipGenID;
522 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
523 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
524}
525
526GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
527 bool renderTarget) {
528 GrSurfaceDesc desc;
529 desc.fWidth = width;
530 desc.fHeight = height;
531 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
robertphillips544b9aa2015-10-28 11:01:41 -0700532 if (!renderTarget || this->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon473addf2015-10-02 07:49:05 -0700533 desc.fConfig = kAlpha_8_GrPixelConfig;
534 } else {
535 desc.fConfig = kRGBA_8888_GrPixelConfig;
536 }
537
robertphillips544b9aa2015-10-28 11:01:41 -0700538 GrTexture* texture = this->resourceProvider()->createApproxTexture(desc, 0);
bsalomon473addf2015-10-02 07:49:05 -0700539 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700540 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000541 }
bsalomon473addf2015-10-02 07:49:05 -0700542 texture->resourcePriv().setUniqueKey(key);
543 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700544}
545
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000546GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700547 GrReducedClip::InitialState initialState,
548 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800549 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000550 const SkIRect& clipSpaceIBounds) {
robertphillips544b9aa2015-10-28 11:01:41 -0700551 GrResourceProvider* resourceProvider = this->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700552 GrUniqueKey key;
553 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
554 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700555 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000556 }
557
robertphillips544b9aa2015-10-28 11:01:41 -0700558 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700559 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
560 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
bsalomon473addf2015-10-02 07:49:05 -0700561 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700562 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563 }
564
joshualitt8059eb92014-12-29 15:10:07 -0800565 // Set the matrix so that rendered clip elements are transformed to mask space from clip
566 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700567 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800568
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000569 // The texture may be larger than necessary, this rect represents the part of the texture
570 // we populate with a rasterization of the clip.
571 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
572
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000573 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
574 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700575 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700576 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
577 true,
bsalomon473addf2015-10-02 07:49:05 -0700578 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000579
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000580 // When we use the stencil in the below loop it is important to have this clip installed.
581 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
582 // pass must not set values outside of this bounds or stencil values outside the rect won't be
583 // cleared.
robertphillips544b9aa2015-10-28 11:01:41 -0700584 const GrClip clip(maskSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800585
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700587 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000589 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000590 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000591 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800592 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800593
joshualitt44701df2015-02-23 14:44:57 -0800594 pipelineBuilder.setClip(clip);
robertphillips544b9aa2015-10-28 11:01:41 -0700595 pipelineBuilder.setRenderTarget(texture->asRenderTarget());
robertphillips423e3372015-10-27 09:23:38 -0700596
robertphillips68737822015-10-29 12:12:21 -0700597 SkASSERT(pipelineBuilder.getStencil().isDisabled());
598 GrPathRenderer* pr = GetPathRenderer(this->getContext(),
599 texture, translate, element);
robertphillips544b9aa2015-10-28 11:01:41 -0700600 if (Element::kRect_Type != element->getType() && !pr) {
601 // useSWOnlyPath should now filter out all cases where gpu-side mask merging would
602 // be performed (i.e., pr would be NULL for a non-rect path). See skbug.com/4519
603 // for rationale and details.
604 SkASSERT(0);
605 continue;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000606 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000607
robertphillips544b9aa2015-10-28 11:01:41 -0700608 // draw directly into the result with the stencil set to make the pixels affected
609 // by the clip shape be non-zero.
610 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
611 kReplace_StencilOp,
612 kReplace_StencilOp,
613 kAlways_StencilFunc,
614 0xffff,
615 0xffff,
616 0xffff);
617 pipelineBuilder.setStencil(kStencilInElement);
618 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
619
620 if (!this->drawElement(&pipelineBuilder, translate, texture, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700621 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700622 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000623 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624
robertphillips544b9aa2015-10-28 11:01:41 -0700625 {
egdaniel8dd688b2015-01-22 10:16:09 -0800626 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700627 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800628
egdaniel8dd688b2015-01-22 10:16:09 -0800629 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000630 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000631 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
632 kZero_StencilOp,
633 kZero_StencilOp,
634 kEqual_StencilFunc,
635 0xffff,
636 0x0000,
637 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800638 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700639
egdaniel87509242014-12-17 13:37:13 -0800640 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700641 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700642 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000643 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800645 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800646
robertphillips@google.come79f3202014-02-11 16:30:21 +0000647 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800648 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800649 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700650 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651 }
652 }
653
bsalomon473addf2015-10-02 07:49:05 -0700654 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655}
656
657////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000658// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000659// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800660bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
661 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700662 GrReducedClip::InitialState initialState,
663 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000664 const SkIRect& clipSpaceIBounds,
665 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700666 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667
robertphillips544b9aa2015-10-28 11:01:41 -0700668 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700669 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670 return false;
671 }
672
egdaniel8dc7c3a2015-04-16 11:22:42 -0700673 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
674 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000675 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
676 SkVector translate = {
677 SkIntToScalar(clipSpaceToStencilOffset.fX),
678 SkIntToScalar(clipSpaceToStencilOffset.fY)
679 };
joshualitt8059eb92014-12-29 15:10:07 -0800680 SkMatrix viewMatrix;
681 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682
bsalomon@google.com9f131742012-12-13 20:43:56 +0000683 // We set the current clip to the bounds so that our recursive draws are scissored to them.
684 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
685 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800686 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687
egdaniel8dc7c3a2015-04-16 11:22:42 -0700688 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000689 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 clipBit = (1 << (clipBit-1));
691
bsalomonb3b9aec2015-09-10 11:16:35 -0700692 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
693 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
695 // walk through each clip element and perform its set op
696 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700697 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000698 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800699
egdaniel8dd688b2015-01-22 10:16:09 -0800700 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800701 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800702 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800703
egdaniel8dd688b2015-01-22 10:16:09 -0800704 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800705
706 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700707 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700708 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800709 }
710
tomhudson@google.com8afae612012-08-14 15:03:35 +0000711 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700713 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000715 // This will be used to determine whether the clip shape can be rendered into the
716 // stencil with arbitrary stencil settings.
717 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718
kkinnunen18996512015-04-26 23:18:49 -0700719 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000720 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000721
halcanary96fcdcc2015-08-27 07:41:13 -0700722 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000723 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000724 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000725 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000727 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000728 element->asPath(&clipPath);
729 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000730 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000731 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000732 }
robertphillips68737822015-10-29 12:12:21 -0700733
734 SkASSERT(pipelineBuilder.getStencil().isDisabled());
735
736 GrPathRenderer::CanDrawPathArgs canDrawArgs;
737 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
738 canDrawArgs.fViewMatrix = &viewMatrix;
739 canDrawArgs.fPath = &clipPath;
740 canDrawArgs.fStroke = &stroke;
741 canDrawArgs.fAntiAlias = false;
742 canDrawArgs.fIsStencilDisabled = pipelineBuilder.getStencil().isDisabled();
743 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
744
745 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
746 GrPathRendererChain::kStencilOnly_DrawType,
747 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700748 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 return false;
750 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 }
752
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 int passes;
754 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
755
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000756 bool canRenderDirectToStencil =
757 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000759 // fill rule, and set operation can
760 // we render the element directly to
761 // stencil bit used for clipping.
762 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
763 canRenderDirectToStencil,
764 clipBit,
765 fillInverted,
766 &passes,
767 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768
769 // draw the element to the client stencil bits if necessary
770 if (!canDrawDirectToClip) {
771 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000772 kIncClamp_StencilOp,
773 kIncClamp_StencilOp,
774 kAlways_StencilFunc,
775 0xffff,
776 0x0000,
777 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000778 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800779 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700780
781 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700782 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700783 GrColor_WHITE,
784 viewMatrix,
785 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000787 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000788 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800789 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700790
791 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700792 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700793 args.fResourceProvider = this->getContext()->resourceProvider();
794 args.fPipelineBuilder = &pipelineBuilder;
795 args.fColor = GrColor_WHITE;
796 args.fViewMatrix = &viewMatrix;
797 args.fPath = &clipPath;
798 args.fStroke = &stroke;
799 args.fAntiAlias = false;
800 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000801 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700802 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700803 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700804 args.fResourceProvider = this->getContext()->resourceProvider();
805 args.fPipelineBuilder = &pipelineBuilder;
806 args.fViewMatrix = &viewMatrix;
807 args.fPath = &clipPath;
808 args.fStroke = &stroke;
809 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000810 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 }
812 }
813 }
814
815 // now we modify the clip bit by rendering either the clip
816 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700817 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700819 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800820
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000822 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700823 // 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 {
bsalomon0aff2fa2015-07-31 06:48:27 -0700829 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700830 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700831 args.fResourceProvider = this->getContext()->resourceProvider();
832 args.fPipelineBuilder = &pipelineBuilder;
833 args.fColor = GrColor_WHITE;
834 args.fViewMatrix = &viewMatrix;
835 args.fPath = &clipPath;
836 args.fStroke = &stroke;
837 args.fAntiAlias = false;
838 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000839 }
840 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000841 // The view matrix is setup to do clip space -> stencil space translation, so
842 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700843 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700844 GrColor_WHITE,
845 viewMatrix,
846 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847 }
848 }
849 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 }
joshualitt7a6184f2014-10-29 18:29:27 -0700851 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 return true;
853}
854
bsalomon@google.com411dad02012-06-05 20:24:20 +0000855// mapping of clip-respecting stencil funcs to normal stencil funcs
856// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000857static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000858 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
859 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
860 // In the Clip Funcs
861 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
862 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
863 kLess_StencilFunc, // kLessIfInClip_StencilFunc
864 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
865 // Special in the clip func that forces user's ref to be 0.
866 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
867 // make ref 0 and do normal nequal.
868 },
869 {// Stencil-Clipping is ENABLED
870 // In the Clip Funcs
871 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
872 // eq stencil clip bit, mask
873 // out user bits.
874
875 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
876 // add stencil bit to mask and ref
877
878 kLess_StencilFunc, // kLessIfInClip_StencilFunc
879 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
880 // for both of these we can add
881 // the clip bit to the mask and
882 // ref and compare as normal
883 // Special in the clip func that forces user's ref to be 0.
884 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
885 // make ref have only the clip bit set
886 // and make comparison be less
887 // 10..0 < 1..user_bits..
888 }
889};
890
bsalomon@google.coma3201942012-06-21 19:58:20 +0000891namespace {
892// Sets the settings to clip against the stencil buffer clip while ignoring the
893// client bits.
894const GrStencilSettings& basic_apply_stencil_clip_settings() {
895 // stencil settings to use when clip is in stencil
896 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
897 kKeep_StencilOp,
898 kKeep_StencilOp,
899 kAlwaysIfInClip_StencilFunc,
900 0x0000,
901 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000902 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000903 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
904}
905}
906
joshualitt5e6ba212015-07-13 07:35:05 -0700907void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800908 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909 // We make two copies of the StencilSettings here (except in the early
910 // exit scenario. One copy from draw state to the stack var. Then another
911 // from the stack var to the gpu. We could make this class hold a ptr to
912 // GrGpu's fStencilSettings and eliminate the stack copy here.
913
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914 // use stencil for clipping if clipping is enabled and the clip
915 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800917
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 // The GrGpu client may not be using the stencil buffer but we may need to
919 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700920 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700921 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000922 settings = basic_apply_stencil_clip_settings();
923 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000924 return;
925 }
926 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700927 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928 }
929
bsalomon@google.coma3201942012-06-21 19:58:20 +0000930 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700931 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
robertphillips544b9aa2015-10-28 11:01:41 -0700932 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700933 if (stencilAttachment) {
934 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935 }
936
robertphillips544b9aa2015-10-28 11:01:41 -0700937 SkASSERT(this->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
938 SkASSERT(this->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700939 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700940 ars->set(&pipelineBuilder);
941 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942}
943
944void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
945 StencilClipMode mode,
946 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000947 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000948
949 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000950 // We assume that this clip manager itself is drawing to the GrGpu and
951 // has already setup the correct values.
952 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000953 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954
bsalomon@google.com411dad02012-06-05 20:24:20 +0000955 unsigned int clipBit = (1 << (stencilBitCnt - 1));
956 unsigned int userBits = clipBit - 1;
957
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
robertphillips544b9aa2015-10-28 11:01:41 -0700959 bool twoSided = this->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000960
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961 bool finished = false;
962 while (!finished) {
963 GrStencilFunc func = settings->func(face);
964 uint16_t writeMask = settings->writeMask(face);
965 uint16_t funcMask = settings->funcMask(face);
966 uint16_t funcRef = settings->funcRef(face);
967
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000968 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969
970 writeMask &= userBits;
971
972 if (func >= kBasicStencilFuncCount) {
973 int respectClip = kRespectClip_StencilClipMode == mode;
974 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 switch (func) {
976 case kAlwaysIfInClip_StencilFunc:
977 funcMask = clipBit;
978 funcRef = clipBit;
979 break;
980 case kEqualIfInClip_StencilFunc:
981 case kLessIfInClip_StencilFunc:
982 case kLEqualIfInClip_StencilFunc:
983 funcMask = (funcMask & userBits) | clipBit;
984 funcRef = (funcRef & userBits) | clipBit;
985 break;
986 case kNonZeroIfInClip_StencilFunc:
987 funcMask = (funcMask & userBits) | clipBit;
988 funcRef = clipBit;
989 break;
990 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000991 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +0000992 }
993 } else {
994 funcMask &= userBits;
995 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000996 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000997 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 gSpecialToBasicStencilFunc[respectClip];
999 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001000 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001001 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001002 funcMask &= userBits;
1003 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001004 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001005
1006 settings->setFunc(face, func);
1007 settings->setWriteMask(face, writeMask);
1008 settings->setFuncMask(face, funcMask);
1009 settings->setFuncRef(face, funcRef);
1010
1011 if (GrStencilSettings::kFront_Face == face) {
1012 face = GrStencilSettings::kBack_Face;
1013 finished = !twoSided;
1014 } else {
1015 finished = true;
1016 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018 if (!twoSided) {
1019 settings->copyFrontSettingsToBack();
1020 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001021}
1022
1023////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001024GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001025 GrReducedClip::InitialState initialState,
1026 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001027 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001028 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001029 GrUniqueKey key;
1030 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips544b9aa2015-10-28 11:01:41 -07001031 GrResourceProvider* resourceProvider = this->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -07001032 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1033 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001034 }
1035
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001036 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1037 // the top left corner of the resulting rect to the top left of the texture.
1038 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1039
robertphillips@google.com2c756812012-05-22 20:28:23 +00001040 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001041
joshualitt8059eb92014-12-29 15:10:07 -08001042 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1043 // space.
1044 SkMatrix translate;
1045 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001046
joshualitt8059eb92014-12-29 15:10:07 -08001047 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001048 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001049 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001050
tfarinabf54e492014-10-23 17:47:18 -07001051 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001052 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001053 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001054
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001055 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1056 // Intersect and reverse difference require modifying pixels outside of the geometry
1057 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1058 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1059 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001060 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001061 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001062 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001063 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001064 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001065 SkPath clipPath;
1066 element->asPath(&clipPath);
1067 clipPath.toggleInverseFillType();
1068 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001069 continue;
1070 }
1071
1072 // The other ops (union, xor, diff) only affect pixels inside
1073 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001074 if (Element::kRect_Type == element->getType()) {
1075 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1076 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001077 SkPath path;
1078 element->asPath(&path);
1079 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001080 }
1081 }
1082
krajcevskiad1dc582014-06-10 15:06:47 -07001083 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001084 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1085 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001086 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001087 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001088 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001089 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001090
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001091 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001092}
1093
robertphillips@google.comf294b772012-04-27 14:29:26 +00001094////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001095
egdaniel8dc7c3a2015-04-16 11:22:42 -07001096void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001097 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001098 if (stencilAttachment) {
1099 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001100 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001101 }
1102}