blob: 3168424cf8715a998b0f0aec94655d9021143b43 [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"
joshualitta8b84992016-01-13 13:35:35 -080023#include "batches/GrRectBatchFactory.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080026#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080027#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
31////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
bsalomon0ba8c242015-10-07 09:20:28 -070034static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000035 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080036 // We use device coords to compute the texture coordinates. We set our matrix to be a
37 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000040 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000042 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070043 return GrTextureDomainEffect::Create(result,
44 mat,
45 GrTextureDomain::MakeTexelDomain(result, domainTexels),
46 GrTextureDomain::kDecal_Mode,
47 GrTextureParams::kNone_FilterMode,
48 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049}
50
joshualitta8b84992016-01-13 13:35:35 -080051static void draw_non_aa_rect(GrDrawTarget* drawTarget,
52 const GrPipelineBuilder& pipelineBuilder,
53 GrColor color,
54 const SkMatrix& viewMatrix,
55 const SkRect& rect) {
56 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
57 nullptr, nullptr));
58 drawTarget->drawBatch(pipelineBuilder, batch);
59}
60
robertphillips3f7357f2015-10-27 07:17:33 -070061// Does the path in 'element' require SW rendering? If so, return true (and,
62// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
63// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070064bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
65 bool isStencilDisabled,
66 const GrRenderTarget* rt,
67 const SkMatrix& viewMatrix,
68 const Element* element,
69 GrPathRenderer** prOut,
70 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070071 if (Element::kRect_Type == element->getType()) {
72 // rects can always be drawn directly w/o using the software path
73 // TODO: skip rrects once we're drawing them directly.
74 if (prOut) {
75 *prOut = nullptr;
76 }
77 return false;
78 } else {
79 // We shouldn't get here with an empty clip element.
80 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070081
robertphillips3f7357f2015-10-27 07:17:33 -070082 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
83 SkPath path;
84 element->asPath(&path);
85 if (path.isInverseFillType()) {
86 path.toggleInverseFillType();
87 }
88 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
89
90 GrPathRendererChain::DrawType type;
91
robertphillips423e3372015-10-27 09:23:38 -070092 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070093 type = element->isAA()
94 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
95 : GrPathRendererChain::kStencilAndColor_DrawType;
96 } else {
97 type = element->isAA()
98 ? GrPathRendererChain::kColorAntiAlias_DrawType
99 : GrPathRendererChain::kColor_DrawType;
100 }
101
robertphillips68737822015-10-29 12:12:21 -0700102 GrPathRenderer::CanDrawPathArgs canDrawArgs;
103 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
104 canDrawArgs.fViewMatrix = &viewMatrix;
105 canDrawArgs.fPath = &path;
106 canDrawArgs.fStroke = &stroke;
107 canDrawArgs.fAntiAlias = element->isAA();
108 canDrawArgs.fIsStencilDisabled = isStencilDisabled;
109 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
110
robertphillips3f7357f2015-10-27 07:17:33 -0700111 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700112 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700113 if (prOut) {
114 *prOut = pr;
115 }
116 return SkToBool(!pr);
117 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000118}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000119
robertphillips423e3372015-10-27 09:23:38 -0700120// Determines whether it is possible to draw the element to both the stencil buffer and the
121// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
122// also returned.
robertphillips68737822015-10-29 12:12:21 -0700123GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context,
124 GrTexture* texture,
125 const SkMatrix& viewMatrix,
126 const SkClipStack::Element* element) {
robertphillips544b9aa2015-10-28 11:01:41 -0700127 GrPathRenderer* pr;
robertphillips423e3372015-10-27 09:23:38 -0700128 static const bool kNeedsStencil = true;
robertphillips68737822015-10-29 12:12:21 -0700129 static const bool kStencilIsDisabled = true;
130 PathNeedsSWRenderer(context,
131 kStencilIsDisabled,
132 texture->asRenderTarget(),
133 viewMatrix,
134 element,
135 &pr,
136 kNeedsStencil);
robertphillips544b9aa2015-10-28 11:01:41 -0700137 return pr;
robertphillips423e3372015-10-27 09:23:38 -0700138}
139
bsalomon69cfe952015-11-30 13:27:47 -0800140GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget, bool debugClipBatchToBounds)
bsalomonc988d2c2015-10-07 09:30:05 -0700141 : fDrawTarget(drawTarget)
bsalomon69cfe952015-11-30 13:27:47 -0800142 , fClipMode(kIgnoreClip_StencilClipMode)
143 , fDebugClipBatchToBounds(debugClipBatchToBounds) {
bsalomonedd77a12015-05-29 09:45:57 -0700144}
145
robertphillips544b9aa2015-10-28 11:01:41 -0700146GrContext* GrClipMaskManager::getContext() {
147 return fDrawTarget->cmmAccess().context();
148}
bsalomonedd77a12015-05-29 09:45:57 -0700149
robertphillips544b9aa2015-10-28 11:01:41 -0700150const GrCaps* GrClipMaskManager::caps() const {
151 return fDrawTarget->caps();
152}
153
154GrResourceProvider* GrClipMaskManager::resourceProvider() {
155 return fDrawTarget->cmmAccess().resourceProvider();
156}
robertphillips@google.comfa662942012-05-17 12:20:22 +0000157/*
158 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
159 * will be used on any element. If so, it returns true to indicate that the
160 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
161 */
joshualitt5e6ba212015-07-13 07:35:05 -0700162bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
robertphillips68737822015-10-29 12:12:21 -0700163 const GrRenderTarget* rt,
joshualitt8059eb92014-12-29 15:10:07 -0800164 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800165 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000166 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000167 // a clip gets complex enough it can just be done in SW regardless
168 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000169
joshualitt8059eb92014-12-29 15:10:07 -0800170 // Set the matrix so that rendered clip elements are transformed to mask space from clip
171 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700172 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800173
tfarinabf54e492014-10-23 17:47:18 -0700174 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000175 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700176
177 SkRegion::Op op = element->getOp();
178 bool invert = element->isInverseFilled();
robertphillips423e3372015-10-27 09:23:38 -0700179 bool needsStencil = invert ||
180 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700181
robertphillips68737822015-10-29 12:12:21 -0700182 if (PathNeedsSWRenderer(this->getContext(), pipelineBuilder.getStencil().isDisabled(),
183 rt, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700184 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000185 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000186 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000188}
189
bsalomon0b5b6b22015-10-14 08:31:34 -0700190bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700191 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700192 SkVector& clipToRTOffset,
193 const SkRect* drawBounds,
194 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700196 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 boundsInClipSpace = *drawBounds;
198 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
199 }
bsalomon0ba8c242015-10-07 09:20:28 -0700200 SkASSERT(elements.count() <= kMaxAnalyticElements);
201 const GrFragmentProcessor* fps[kMaxAnalyticElements];
202 for (int i = 0; i < kMaxAnalyticElements; ++i) {
203 fps[i] = nullptr;
204 }
205 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700206 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700208 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 SkRegion::Op op = iter.get()->getOp();
210 bool invert;
211 bool skip = false;
212 switch (op) {
213 case SkRegion::kReplace_Op:
214 SkASSERT(iter.get() == elements.head());
215 // Fallthrough, handled same as intersect.
216 case SkRegion::kIntersect_Op:
217 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700218 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 skip = true;
220 }
221 break;
222 case SkRegion::kDifference_Op:
223 invert = true;
224 // We don't currently have a cheap test for whether a rect is fully outside an
225 // element's primitive, so don't attempt to set skip.
226 break;
227 default:
228 failed = true;
229 break;
230 }
231 if (failed) {
232 break;
233 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700235 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800236 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700237 if (abortIfAA) {
238 failed = true;
239 break;
240 }
joshualittb0a8a372014-09-23 09:50:21 -0700241 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700242 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700244 edgeType =
245 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 }
bsalomona912dde2015-10-14 15:01:50 -0700247
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 switch (iter.get()->getType()) {
249 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700250 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
251 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000252 break;
253 case SkClipStack::Element::kRRect_Type: {
254 SkRRect rrect = iter.get()->getRRect();
255 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700256 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 break;
258 }
259 case SkClipStack::Element::kRect_Type: {
260 SkRect rect = iter.get()->getRect();
261 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700262 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263 break;
264 }
265 default:
266 break;
267 }
bsalomon0ba8c242015-10-07 09:20:28 -0700268 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000269 failed = true;
270 break;
271 }
bsalomon0ba8c242015-10-07 09:20:28 -0700272 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000273 }
mtklein217daa72014-07-02 12:55:21 -0700274 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 }
276
bsalomon0b5b6b22015-10-14 08:31:34 -0700277 *resultFP = nullptr;
278 if (!failed && fpCnt) {
279 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000280 }
bsalomon0ba8c242015-10-07 09:20:28 -0700281 for (int i = 0; i < fpCnt; ++i) {
282 fps[i]->unref();
283 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700284 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000285}
286
bsalomon69cfe952015-11-30 13:27:47 -0800287static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) {
288 switch (clip.clipType()) {
289 case GrClip::kClipStack_ClipType: {
290 SkClipStack* stack = new SkClipStack;
291 *stack = *clip.clipStack();
292 // The stack is actually in clip space not device space.
293 SkRect clipRect = devRect;
294 SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) };
295 clipRect.offset(origin);
296 SkIRect iclipRect;
297 clipRect.roundOut(&iclipRect);
298 clipRect = SkRect::Make(iclipRect);
299 stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false);
300 out->setClipStack(stack, &clip.origin());
301 break;
302 }
303 case GrClip::kWideOpen_ClipType:
304 *out = GrClip(devRect);
305 break;
306 case GrClip::kIRect_ClipType: {
307 SkIRect intersect;
308 devRect.roundOut(&intersect);
309 if (intersect.intersect(clip.irect())) {
310 *out = GrClip(intersect);
311 } else {
312 *out = clip;
313 }
314 break;
315 }
316 }
317}
318
robertphillips@google.comf294b772012-04-27 14:29:26 +0000319////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000320// sort out what kind of clip mask needs to be created: alpha, stencil,
321// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700322bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800323 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon0ba8c242015-10-07 09:20:28 -0700324 const SkRect* devBounds,
325 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700326 if (kRespectClip_StencilClipMode == fClipMode) {
327 fClipMode = kIgnoreClip_StencilClipMode;
328 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000329
bsalomonf045d602015-11-18 19:01:12 -0800330 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800331 int32_t genID = 0;
332 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800334 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700335 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000336
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000337 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700338 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000339
joshualitt44701df2015-02-23 14:44:57 -0800340 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon69cfe952015-11-30 13:27:47 -0800341 GrClip devBoundsClip;
342 bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds;
343 if (doDevBoundsClip) {
344 add_rect_to_clip(pipelineBuilder.clip(), *devBounds, &devBoundsClip);
345 }
346 const GrClip& clip = doDevBoundsClip ? devBoundsClip : pipelineBuilder.clip();
347
bsalomon96e02a82015-03-06 07:13:01 -0800348 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800349 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000350 return true;
351 }
352
bsalomon96e02a82015-03-06 07:13:01 -0800353 // The clip mask manager always draws with a single IRect so we special case that logic here
354 // Image filters just use a rect, so we also special case that logic
355 switch (clip.clipType()) {
356 case GrClip::kWideOpen_ClipType:
357 SkFAIL("Should have caught this with clip.isWideOpen()");
358 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800359 case GrClip::kIRect_ClipType: {
360 SkIRect scissor = clip.irect();
361 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700362 out->fScissorState.set(scissor);
bsalomon9ce30e12015-03-06 08:42:34 -0800363 this->setPipelineBuilderStencil(pipelineBuilder, ars);
364 return true;
365 }
366 return false;
367 }
bsalomon96e02a82015-03-06 07:13:01 -0800368 case GrClip::kClipStack_ClipType: {
369 clipSpaceRTIBounds.offset(clip.origin());
bsalomondb4758c2015-11-23 11:14:20 -0800370 SkIRect clipSpaceReduceQueryBounds;
Brian Salomon362c9002015-11-30 17:02:50 -0500371#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 1
372 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
bsalomondb4758c2015-11-23 11:14:20 -0800373 SkIRect devIBounds = devBounds->roundOut();
374 devIBounds.offset(clip.origin());
375 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
376 return false;
377 }
378 } else {
379 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
380 }
bsalomon96e02a82015-03-06 07:13:01 -0800381 GrReducedClip::ReduceClipStack(*clip.clipStack(),
bsalomondb4758c2015-11-23 11:14:20 -0800382 clipSpaceReduceQueryBounds,
bsalomon96e02a82015-03-06 07:13:01 -0800383 &elements,
384 &genID,
385 &initialState,
386 &clipSpaceIBounds,
387 &requiresAA);
388 if (elements.isEmpty()) {
389 if (GrReducedClip::kAllIn_InitialState == initialState) {
390 if (clipSpaceIBounds == clipSpaceRTIBounds) {
391 this->setPipelineBuilderStencil(pipelineBuilder, ars);
392 return true;
393 }
394 } else {
395 return false;
396 }
397 }
398 } break;
399 }
400
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000401 // An element count of 4 was chosen because of the common pattern in Blink of:
402 // isect RR
403 // diff RR
404 // isect convex_poly
405 // isect convex_poly
406 // when drawing rounded div borders. This could probably be tuned based on a
407 // configuration's relative costs of switching RTs to generate a mask vs
408 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700409 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800410 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
411 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800412 // When there are multiple samples we want to do per-sample clipping, not compute a
413 // fractional pixel coverage.
414 bool disallowAnalyticAA = rt->isUnifiedMultisampled() || pipelineBuilder.hasMixedSamples();
bsalomon0ba8c242015-10-07 09:20:28 -0700415 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000416 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700417 (requiresAA &&
418 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
419 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700420 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800421 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700422 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700423 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700424 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000425 }
egdaniel8dd688b2015-01-22 10:16:09 -0800426 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700427 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000428 return true;
429 }
430 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000431
cdaltonede75742015-11-11 15:27:57 -0800432 // If the stencil buffer is multisampled we can use it to do everything.
433 if (!rt->isStencilBufferMultisampled() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700434 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435
joshualitt8059eb92014-12-29 15:10:07 -0800436 // The top-left of the mask corresponds to the top-left corner of the bounds.
437 SkVector clipToMaskOffset = {
438 SkIntToScalar(-clipSpaceIBounds.fLeft),
439 SkIntToScalar(-clipSpaceIBounds.fTop)
440 };
441
robertphillips68737822015-10-29 12:12:21 -0700442 if (this->useSWOnlyPath(pipelineBuilder, rt, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // The clip geometry is complex enough that it will be more efficient to create it
444 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700445 result.reset(this->createSoftwareClipMask(genID,
446 initialState,
447 elements,
448 clipToMaskOffset,
449 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700451 result.reset(this->createAlphaClipMask(genID,
452 initialState,
453 elements,
454 clipToMaskOffset,
455 clipSpaceIBounds));
robertphillips3f7357f2015-10-27 07:17:33 -0700456 // If createAlphaClipMask fails it means useSWOnlyPath has a bug
457 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000458 }
459
bsalomon49f085d2014-09-05 13:34:00 -0700460 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000461 // The mask's top left coord should be pinned to the rounded-out top left corner of
462 // clipSpace bounds. We determine the mask's position WRT to the render target here.
463 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800464 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700465 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800466 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000467 return true;
468 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000469 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000472 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800473 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800474 this->createStencilClipMask(rt,
475 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000476 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 elements,
478 clipSpaceIBounds,
479 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000480
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000481 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
482 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
483 // use both stencil and scissor test to the bounds for the final draw.
484 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
485 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700486 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800487 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000488 return true;
489}
490
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000491namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800493// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
494void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
495 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800496 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800497 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000499}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000500
501////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800502bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800503 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800504 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000505 const SkClipStack::Element* element,
506 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507
robertphillips68737822015-10-29 12:12:21 -0700508 GrRenderTarget* rt = target->asRenderTarget();
509 pipelineBuilder->setRenderTarget(rt);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000510
egdaniel87509242014-12-17 13:37:13 -0800511 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
512 // which ignores color.
513 GrColor color = GrColor_WHITE;
514
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000515 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000516 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000517 case Element::kEmpty_Type:
518 SkDEBUGFAIL("Should never get here with an empty element.");
519 break;
joshualitta8b84992016-01-13 13:35:35 -0800520 case Element::kRect_Type: {
joshualittb0a8a372014-09-23 09:50:21 -0700521 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
522 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000523 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800524 SkRect devRect = element->getRect();
525 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700526
joshualitta8b84992016-01-13 13:35:35 -0800527 SkAutoTUnref<GrDrawBatch> batch(
528 GrRectBatchFactory::CreateAAFill(color, viewMatrix, element->getRect(),
529 devRect));
530
531 fDrawTarget->drawBatch(*pipelineBuilder, batch);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000532 } else {
joshualitta8b84992016-01-13 13:35:35 -0800533 draw_non_aa_rect(fDrawTarget, *pipelineBuilder, color, viewMatrix,
534 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000535 }
536 return true;
joshualitta8b84992016-01-13 13:35:35 -0800537 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000538 default: {
539 SkPath path;
540 element->asPath(&path);
541 if (path.isInverseFillType()) {
542 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000543 }
kkinnunen18996512015-04-26 23:18:49 -0700544 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700545 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000546 GrPathRendererChain::DrawType type;
547 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
548 GrPathRendererChain::kColor_DrawType;
robertphillips68737822015-10-29 12:12:21 -0700549
550 GrPathRenderer::CanDrawPathArgs canDrawArgs;
551 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
552 canDrawArgs.fViewMatrix = &viewMatrix;
553 canDrawArgs.fPath = &path;
554 canDrawArgs.fStroke = &stroke;
555 canDrawArgs.fAntiAlias = element->isAA();;
556 canDrawArgs.fIsStencilDisabled = pipelineBuilder->getStencil().isDisabled();
557 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
558
559 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000560 }
halcanary96fcdcc2015-08-27 07:41:13 -0700561 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 return false;
563 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700564 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700565 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700566 args.fResourceProvider = this->getContext()->resourceProvider();
567 args.fPipelineBuilder = pipelineBuilder;
568 args.fColor = color;
569 args.fViewMatrix = &viewMatrix;
570 args.fPath = &path;
571 args.fStroke = &stroke;
572 args.fAntiAlias = element->isAA();
573 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000574 break;
575 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576 }
577 return true;
578}
579
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700581// Create a 8-bit clip mask in alpha
582
583static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
584 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
585 GrUniqueKey::Builder builder(key, kDomain, 3);
586 builder[0] = clipGenID;
587 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
588 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
589}
590
591GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
592 bool renderTarget) {
593 GrSurfaceDesc desc;
594 desc.fWidth = width;
595 desc.fHeight = height;
596 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
robertphillips544b9aa2015-10-28 11:01:41 -0700597 if (!renderTarget || this->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon473addf2015-10-02 07:49:05 -0700598 desc.fConfig = kAlpha_8_GrPixelConfig;
599 } else {
600 desc.fConfig = kRGBA_8888_GrPixelConfig;
601 }
602
robertphillips544b9aa2015-10-28 11:01:41 -0700603 GrTexture* texture = this->resourceProvider()->createApproxTexture(desc, 0);
bsalomon473addf2015-10-02 07:49:05 -0700604 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700605 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000606 }
bsalomon473addf2015-10-02 07:49:05 -0700607 texture->resourcePriv().setUniqueKey(key);
608 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700609}
610
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000611GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700612 GrReducedClip::InitialState initialState,
613 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800614 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 const SkIRect& clipSpaceIBounds) {
robertphillips544b9aa2015-10-28 11:01:41 -0700616 GrResourceProvider* resourceProvider = this->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700617 GrUniqueKey key;
618 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
619 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700620 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000621 }
622
robertphillips544b9aa2015-10-28 11:01:41 -0700623 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700624 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
625 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
bsalomon473addf2015-10-02 07:49:05 -0700626 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700627 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628 }
629
joshualitt8059eb92014-12-29 15:10:07 -0800630 // Set the matrix so that rendered clip elements are transformed to mask space from clip
631 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700632 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800633
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000634 // The texture may be larger than necessary, this rect represents the part of the texture
635 // we populate with a rasterization of the clip.
636 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
637
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000638 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
639 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700640 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700641 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
642 true,
bsalomon473addf2015-10-02 07:49:05 -0700643 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000644
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000645 // When we use the stencil in the below loop it is important to have this clip installed.
646 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
647 // pass must not set values outside of this bounds or stencil values outside the rect won't be
648 // cleared.
robertphillips544b9aa2015-10-28 11:01:41 -0700649 const GrClip clip(maskSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800650
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700652 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000653 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000654 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000655 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000656 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
joshualitt9853cce2014-11-17 14:22:48 -0800657
robertphillips68737822015-10-29 12:12:21 -0700658 GrPathRenderer* pr = GetPathRenderer(this->getContext(),
659 texture, translate, element);
robertphillips544b9aa2015-10-28 11:01:41 -0700660 if (Element::kRect_Type != element->getType() && !pr) {
661 // useSWOnlyPath should now filter out all cases where gpu-side mask merging would
halcanary6950de62015-11-07 05:29:00 -0800662 // be performed (i.e., pr would be NULL for a non-rect path). See https://bug.skia.org/4519
robertphillips544b9aa2015-10-28 11:01:41 -0700663 // for rationale and details.
664 SkASSERT(0);
665 continue;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000666 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000667
robertphillips13391dd2015-10-30 05:15:11 -0700668 {
669 GrPipelineBuilder pipelineBuilder;
robertphillips544b9aa2015-10-28 11:01:41 -0700670
robertphillips13391dd2015-10-30 05:15:11 -0700671 pipelineBuilder.setClip(clip);
672 pipelineBuilder.setRenderTarget(texture->asRenderTarget());
673 SkASSERT(pipelineBuilder.getStencil().isDisabled());
674
675 // draw directly into the result with the stencil set to make the pixels affected
676 // by the clip shape be non-zero.
677 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
678 kReplace_StencilOp,
679 kReplace_StencilOp,
680 kAlways_StencilFunc,
681 0xffff,
682 0xffff,
683 0xffff);
684 pipelineBuilder.setStencil(kStencilInElement);
685 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
686
687 if (!this->drawElement(&pipelineBuilder, translate, texture, element, pr)) {
688 texture->resourcePriv().removeUniqueKey();
689 return nullptr;
690 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000691 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692
robertphillips544b9aa2015-10-28 11:01:41 -0700693 {
egdaniel8dd688b2015-01-22 10:16:09 -0800694 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700695 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800696
egdaniel8dd688b2015-01-22 10:16:09 -0800697 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000698 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000699 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
700 kZero_StencilOp,
701 kZero_StencilOp,
702 kEqual_StencilFunc,
703 0xffff,
704 0x0000,
705 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800706 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700707
egdaniel87509242014-12-17 13:37:13 -0800708 // The color passed in here does not matter since the coverageSetOpXP won't read it.
joshualitta8b84992016-01-13 13:35:35 -0800709 draw_non_aa_rect(fDrawTarget, backgroundPipelineBuilder, GrColor_WHITE, translate,
710 SkRect::Make(clipSpaceIBounds));
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000711 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000712 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800713 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800714
robertphillips@google.come79f3202014-02-11 16:30:21 +0000715 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800716 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800717 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700718 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000719 }
720 }
721
bsalomon473addf2015-10-02 07:49:05 -0700722 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000723}
724
725////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000726// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000727// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800728bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
729 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700730 GrReducedClip::InitialState initialState,
731 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000732 const SkIRect& clipSpaceIBounds,
733 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700734 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735
robertphillips544b9aa2015-10-28 11:01:41 -0700736 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700737 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 return false;
739 }
740
egdaniel8dc7c3a2015-04-16 11:22:42 -0700741 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
742 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000743 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
744 SkVector translate = {
745 SkIntToScalar(clipSpaceToStencilOffset.fX),
746 SkIntToScalar(clipSpaceToStencilOffset.fY)
747 };
joshualitt8059eb92014-12-29 15:10:07 -0800748 SkMatrix viewMatrix;
749 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750
bsalomon@google.com9f131742012-12-13 20:43:56 +0000751 // We set the current clip to the bounds so that our recursive draws are scissored to them.
752 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
753 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800754 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755
egdaniel8dc7c3a2015-04-16 11:22:42 -0700756 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000757 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 clipBit = (1 << (clipBit-1));
759
bsalomonb3b9aec2015-09-10 11:16:35 -0700760 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
761 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762
763 // walk through each clip element and perform its set op
764 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700765 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000766 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800767
egdaniel8dd688b2015-01-22 10:16:09 -0800768 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800769 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800770 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800771
egdaniel8dd688b2015-01-22 10:16:09 -0800772 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800773
774 // if the target is MSAA then we want MSAA enabled when the clip is soft
cdaltonede75742015-11-11 15:27:57 -0800775 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700776 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800777 }
778
tomhudson@google.com8afae612012-08-14 15:03:35 +0000779 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700781 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000783 // This will be used to determine whether the clip shape can be rendered into the
784 // stencil with arbitrary stencil settings.
785 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786
kkinnunen18996512015-04-26 23:18:49 -0700787 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000788 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000789
halcanary96fcdcc2015-08-27 07:41:13 -0700790 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000791 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000792 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000793 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000794 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000795 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000796 element->asPath(&clipPath);
797 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000798 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000799 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000800 }
robertphillips68737822015-10-29 12:12:21 -0700801
802 SkASSERT(pipelineBuilder.getStencil().isDisabled());
803
804 GrPathRenderer::CanDrawPathArgs canDrawArgs;
805 canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps();
806 canDrawArgs.fViewMatrix = &viewMatrix;
807 canDrawArgs.fPath = &clipPath;
808 canDrawArgs.fStroke = &stroke;
809 canDrawArgs.fAntiAlias = false;
810 canDrawArgs.fIsStencilDisabled = pipelineBuilder.getStencil().isDisabled();
811 canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled();
812
813 pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false,
814 GrPathRendererChain::kStencilOnly_DrawType,
815 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700816 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 return false;
818 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 }
820
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 int passes;
822 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
823
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000824 bool canRenderDirectToStencil =
825 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000827 // fill rule, and set operation can
828 // we render the element directly to
829 // stencil bit used for clipping.
830 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
831 canRenderDirectToStencil,
832 clipBit,
833 fillInverted,
834 &passes,
835 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836
837 // draw the element to the client stencil bits if necessary
838 if (!canDrawDirectToClip) {
839 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000840 kIncClamp_StencilOp,
841 kIncClamp_StencilOp,
842 kAlways_StencilFunc,
843 0xffff,
844 0x0000,
845 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000846 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800847 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700848
joshualitta8b84992016-01-13 13:35:35 -0800849 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
850 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000852 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000853 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800854 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700855
856 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700857 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700858 args.fResourceProvider = this->getContext()->resourceProvider();
859 args.fPipelineBuilder = &pipelineBuilder;
860 args.fColor = GrColor_WHITE;
861 args.fViewMatrix = &viewMatrix;
862 args.fPath = &clipPath;
863 args.fStroke = &stroke;
864 args.fAntiAlias = false;
865 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000866 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700867 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700868 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700869 args.fResourceProvider = this->getContext()->resourceProvider();
870 args.fPipelineBuilder = &pipelineBuilder;
871 args.fViewMatrix = &viewMatrix;
872 args.fPath = &clipPath;
873 args.fStroke = &stroke;
874 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000875 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 }
877 }
878 }
879
880 // now we modify the clip bit by rendering either the clip
881 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700882 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000883 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700884 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800885
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000887 if (Element::kRect_Type == element->getType()) {
joshualitta8b84992016-01-13 13:35:35 -0800888 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
889 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000890 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700891 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700892 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700893 args.fResourceProvider = this->getContext()->resourceProvider();
894 args.fPipelineBuilder = &pipelineBuilder;
895 args.fColor = GrColor_WHITE;
896 args.fViewMatrix = &viewMatrix;
897 args.fPath = &clipPath;
898 args.fStroke = &stroke;
899 args.fAntiAlias = false;
900 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000901 }
902 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000903 // The view matrix is setup to do clip space -> stencil space translation, so
904 // draw rect in clip space.
joshualitta8b84992016-01-13 13:35:35 -0800905 draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix,
906 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000907 }
908 }
909 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000910 }
joshualitt7a6184f2014-10-29 18:29:27 -0700911 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000912 return true;
913}
914
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915// mapping of clip-respecting stencil funcs to normal stencil funcs
916// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000917static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000918 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
919 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
920 // In the Clip Funcs
921 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
922 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
923 kLess_StencilFunc, // kLessIfInClip_StencilFunc
924 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
925 // Special in the clip func that forces user's ref to be 0.
926 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
927 // make ref 0 and do normal nequal.
928 },
929 {// Stencil-Clipping is ENABLED
930 // In the Clip Funcs
931 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
932 // eq stencil clip bit, mask
933 // out user bits.
934
935 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
936 // add stencil bit to mask and ref
937
938 kLess_StencilFunc, // kLessIfInClip_StencilFunc
939 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
940 // for both of these we can add
941 // the clip bit to the mask and
942 // ref and compare as normal
943 // Special in the clip func that forces user's ref to be 0.
944 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
945 // make ref have only the clip bit set
946 // and make comparison be less
947 // 10..0 < 1..user_bits..
948 }
949};
950
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951namespace {
952// Sets the settings to clip against the stencil buffer clip while ignoring the
953// client bits.
954const GrStencilSettings& basic_apply_stencil_clip_settings() {
955 // stencil settings to use when clip is in stencil
956 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
957 kKeep_StencilOp,
958 kKeep_StencilOp,
959 kAlwaysIfInClip_StencilFunc,
960 0x0000,
961 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000962 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000963 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
964}
965}
966
joshualitt5e6ba212015-07-13 07:35:05 -0700967void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800968 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000969 // We make two copies of the StencilSettings here (except in the early
970 // exit scenario. One copy from draw state to the stack var. Then another
971 // from the stack var to the gpu. We could make this class hold a ptr to
972 // GrGpu's fStencilSettings and eliminate the stack copy here.
973
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974 // use stencil for clipping if clipping is enabled and the clip
975 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800977
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 // The GrGpu client may not be using the stencil buffer but we may need to
979 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700980 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700981 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982 settings = basic_apply_stencil_clip_settings();
983 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000984 return;
985 }
986 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700987 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000988 }
989
bsalomon@google.coma3201942012-06-21 19:58:20 +0000990 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700991 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
robertphillips544b9aa2015-10-28 11:01:41 -0700992 GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700993 if (stencilAttachment) {
994 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995 }
996
robertphillips544b9aa2015-10-28 11:01:41 -0700997 SkASSERT(this->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
998 SkASSERT(this->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700999 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -07001000 ars->set(&pipelineBuilder);
1001 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001002}
1003
1004void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1005 StencilClipMode mode,
1006 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001007 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001008
1009 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001010 // We assume that this clip manager itself is drawing to the GrGpu and
1011 // has already setup the correct values.
1012 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1016 unsigned int userBits = clipBit - 1;
1017
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
robertphillips544b9aa2015-10-28 11:01:41 -07001019 bool twoSided = this->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020
bsalomon@google.coma3201942012-06-21 19:58:20 +00001021 bool finished = false;
1022 while (!finished) {
1023 GrStencilFunc func = settings->func(face);
1024 uint16_t writeMask = settings->writeMask(face);
1025 uint16_t funcMask = settings->funcMask(face);
1026 uint16_t funcRef = settings->funcRef(face);
1027
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001028 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029
1030 writeMask &= userBits;
1031
1032 if (func >= kBasicStencilFuncCount) {
1033 int respectClip = kRespectClip_StencilClipMode == mode;
1034 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001035 switch (func) {
1036 case kAlwaysIfInClip_StencilFunc:
1037 funcMask = clipBit;
1038 funcRef = clipBit;
1039 break;
1040 case kEqualIfInClip_StencilFunc:
1041 case kLessIfInClip_StencilFunc:
1042 case kLEqualIfInClip_StencilFunc:
1043 funcMask = (funcMask & userBits) | clipBit;
1044 funcRef = (funcRef & userBits) | clipBit;
1045 break;
1046 case kNonZeroIfInClip_StencilFunc:
1047 funcMask = (funcMask & userBits) | clipBit;
1048 funcRef = clipBit;
1049 break;
1050 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001051 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001052 }
1053 } else {
1054 funcMask &= userBits;
1055 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001056 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001057 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001058 gSpecialToBasicStencilFunc[respectClip];
1059 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001060 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001061 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001062 funcMask &= userBits;
1063 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001064 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001065
1066 settings->setFunc(face, func);
1067 settings->setWriteMask(face, writeMask);
1068 settings->setFuncMask(face, funcMask);
1069 settings->setFuncRef(face, funcRef);
1070
1071 if (GrStencilSettings::kFront_Face == face) {
1072 face = GrStencilSettings::kBack_Face;
1073 finished = !twoSided;
1074 } else {
1075 finished = true;
1076 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001077 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001078 if (!twoSided) {
1079 settings->copyFrontSettingsToBack();
1080 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001081}
1082
1083////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001084GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001085 GrReducedClip::InitialState initialState,
1086 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001087 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001088 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001089 GrUniqueKey key;
1090 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips544b9aa2015-10-28 11:01:41 -07001091 GrResourceProvider* resourceProvider = this->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -07001092 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1093 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001094 }
1095
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001096 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1097 // the top left corner of the resulting rect to the top left of the texture.
1098 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1099
robertphillips@google.com2c756812012-05-22 20:28:23 +00001100 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001101
joshualitt8059eb92014-12-29 15:10:07 -08001102 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1103 // space.
1104 SkMatrix translate;
1105 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001106
joshualitt8059eb92014-12-29 15:10:07 -08001107 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001108 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001109 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001110
tfarinabf54e492014-10-23 17:47:18 -07001111 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001112 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001113 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001115 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1116 // Intersect and reverse difference require modifying pixels outside of the geometry
1117 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1118 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1119 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001120 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001121 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001123 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001124 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001125 SkPath clipPath;
1126 element->asPath(&clipPath);
1127 clipPath.toggleInverseFillType();
1128 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001129 continue;
1130 }
1131
1132 // The other ops (union, xor, diff) only affect pixels inside
1133 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001134 if (Element::kRect_Type == element->getType()) {
1135 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1136 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001137 SkPath path;
1138 element->asPath(&path);
1139 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001140 }
1141 }
1142
krajcevskiad1dc582014-06-10 15:06:47 -07001143 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001144 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1145 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001146 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001147 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001148 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001149 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001150
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001151 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001152}
1153
robertphillips@google.comf294b772012-04-27 14:29:26 +00001154////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001155
egdaniel8dc7c3a2015-04-16 11:22:42 -07001156void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001157 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001158 if (stencilAttachment) {
1159 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001160 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001161 }
1162}