blob: ff00a94f5aaebee244e3269da47199ff454a05c7 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
11#include "GrDrawTarget.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkTLazy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080022#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080024#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080025#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000031// stage matrix this also alters the vertex layout
bsalomon0ba8c242015-10-07 09:20:28 -070032static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000033 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080034 // We use device coords to compute the texture coordinates. We set our matrix to be a
35 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000038 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000040 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon0ba8c242015-10-07 09:20:28 -070041 return GrTextureDomainEffect::Create(result,
42 mat,
43 GrTextureDomain::MakeTexelDomain(result, domainTexels),
44 GrTextureDomain::kDecal_Mode,
45 GrTextureParams::kNone_FilterMode,
46 kDevice_GrCoordSet);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
robertphillips3f7357f2015-10-27 07:17:33 -070049// Does the path in 'element' require SW rendering? If so, return true (and,
50// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
51// 'prOut' to the non-SW path renderer that will do the job).
bsalomon0ba8c242015-10-07 09:20:28 -070052static bool path_needs_SW_renderer(GrContext* context,
bsalomon0ba8c242015-10-07 09:20:28 -070053 const GrPipelineBuilder& pipelineBuilder,
54 const SkMatrix& viewMatrix,
robertphillips3f7357f2015-10-27 07:17:33 -070055 const Element* element,
56 GrPathRenderer** prOut,
robertphillips423e3372015-10-27 09:23:38 -070057 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070058 if (Element::kRect_Type == element->getType()) {
59 // rects can always be drawn directly w/o using the software path
60 // TODO: skip rrects once we're drawing them directly.
61 if (prOut) {
62 *prOut = nullptr;
63 }
64 return false;
65 } else {
66 // We shouldn't get here with an empty clip element.
67 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070068
robertphillips3f7357f2015-10-27 07:17:33 -070069 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
70 SkPath path;
71 element->asPath(&path);
72 if (path.isInverseFillType()) {
73 path.toggleInverseFillType();
74 }
75 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
76
77 GrPathRendererChain::DrawType type;
78
robertphillips423e3372015-10-27 09:23:38 -070079 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070080 type = element->isAA()
81 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
82 : GrPathRendererChain::kStencilAndColor_DrawType;
83 } else {
84 type = element->isAA()
85 ? GrPathRendererChain::kColorAntiAlias_DrawType
86 : GrPathRendererChain::kColor_DrawType;
87 }
88
89 // the 'false' parameter disallows use of the SW path renderer
robertphillipscf10b5a2015-10-27 07:53:35 -070090 GrPathRenderer* pr = context->getPathRenderer(pipelineBuilder, viewMatrix, path,
robertphillips3f7357f2015-10-27 07:17:33 -070091 stroke, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -070092 if (prOut) {
93 *prOut = pr;
94 }
95 return SkToBool(!pr);
96 }
robertphillips@google.come79f3202014-02-11 16:30:21 +000097}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000098
robertphillips423e3372015-10-27 09:23:38 -070099// Determines whether it is possible to draw the element to both the stencil buffer and the
100// alpha mask simultaneously. If so and the element is a path a compatible path renderer is
101// also returned.
102static bool can_stencil_and_draw_element(GrContext* context,
103 GrPipelineBuilder* pipelineBuilder,
104 GrTexture* texture,
105 const SkMatrix& viewMatrix,
106 const SkClipStack::Element* element,
107 GrPathRenderer** pr) {
108 pipelineBuilder->setRenderTarget(texture->asRenderTarget());
109
110 static const bool kNeedsStencil = true;
111 return !path_needs_SW_renderer(context,
112 *pipelineBuilder,
113 viewMatrix,
114 element,
115 pr,
116 kNeedsStencil);
117}
118
bsalomonb3b9aec2015-09-10 11:16:35 -0700119GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget)
bsalomonc988d2c2015-10-07 09:30:05 -0700120 : fDrawTarget(drawTarget)
bsalomonedd77a12015-05-29 09:45:57 -0700121 , fClipMode(kIgnoreClip_StencilClipMode) {
122}
123
bsalomonb3b9aec2015-09-10 11:16:35 -0700124GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); }
bsalomonedd77a12015-05-29 09:45:57 -0700125
robertphillips@google.comfa662942012-05-17 12:20:22 +0000126/*
127 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
128 * will be used on any element. If so, it returns true to indicate that the
129 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
130 */
joshualitt5e6ba212015-07-13 07:35:05 -0700131bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800132 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800133 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000134 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000135 // a clip gets complex enough it can just be done in SW regardless
136 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000137
joshualitt8059eb92014-12-29 15:10:07 -0800138 // Set the matrix so that rendered clip elements are transformed to mask space from clip
139 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700140 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800141
tfarinabf54e492014-10-23 17:47:18 -0700142 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000143 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700144
145 SkRegion::Op op = element->getOp();
146 bool invert = element->isInverseFilled();
robertphillips423e3372015-10-27 09:23:38 -0700147 bool needsStencil = invert ||
148 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700149
150 if (path_needs_SW_renderer(this->getContext(), pipelineBuilder, translate,
robertphillips423e3372015-10-27 09:23:38 -0700151 element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700152 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000153 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000154 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000155 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000156}
157
bsalomon0b5b6b22015-10-14 08:31:34 -0700158bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements,
bsalomona912dde2015-10-14 15:01:50 -0700159 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700160 SkVector& clipToRTOffset,
161 const SkRect* drawBounds,
162 const GrFragmentProcessor** resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000163 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700164 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000165 boundsInClipSpace = *drawBounds;
166 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
167 }
bsalomon0ba8c242015-10-07 09:20:28 -0700168 SkASSERT(elements.count() <= kMaxAnalyticElements);
169 const GrFragmentProcessor* fps[kMaxAnalyticElements];
170 for (int i = 0; i < kMaxAnalyticElements; ++i) {
171 fps[i] = nullptr;
172 }
173 int fpCnt = 0;
tfarinabf54e492014-10-23 17:47:18 -0700174 GrReducedClip::ElementList::Iter iter(elements);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000175 bool failed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700176 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 SkRegion::Op op = iter.get()->getOp();
178 bool invert;
179 bool skip = false;
180 switch (op) {
181 case SkRegion::kReplace_Op:
182 SkASSERT(iter.get() == elements.head());
183 // Fallthrough, handled same as intersect.
184 case SkRegion::kIntersect_Op:
185 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700186 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000187 skip = true;
188 }
189 break;
190 case SkRegion::kDifference_Op:
191 invert = true;
192 // We don't currently have a cheap test for whether a rect is fully outside an
193 // element's primitive, so don't attempt to set skip.
194 break;
195 default:
196 failed = true;
197 break;
198 }
199 if (failed) {
200 break;
201 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700203 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800204 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700205 if (abortIfAA) {
206 failed = true;
207 break;
208 }
joshualittb0a8a372014-09-23 09:50:21 -0700209 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700210 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700212 edgeType =
213 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
bsalomona912dde2015-10-14 15:01:50 -0700215
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 switch (iter.get()->getType()) {
217 case SkClipStack::Element::kPath_Type:
bsalomon0ba8c242015-10-07 09:20:28 -0700218 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
219 &clipToRTOffset);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000220 break;
221 case SkClipStack::Element::kRRect_Type: {
222 SkRRect rrect = iter.get()->getRRect();
223 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700224 fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225 break;
226 }
227 case SkClipStack::Element::kRect_Type: {
228 SkRect rect = iter.get()->getRect();
229 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700230 fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 break;
232 }
233 default:
234 break;
235 }
bsalomon0ba8c242015-10-07 09:20:28 -0700236 if (!fps[fpCnt]) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 failed = true;
238 break;
239 }
bsalomon0ba8c242015-10-07 09:20:28 -0700240 fpCnt++;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 }
mtklein217daa72014-07-02 12:55:21 -0700242 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 }
244
bsalomon0b5b6b22015-10-14 08:31:34 -0700245 *resultFP = nullptr;
246 if (!failed && fpCnt) {
247 *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 }
bsalomon0ba8c242015-10-07 09:20:28 -0700249 for (int i = 0; i < fpCnt; ++i) {
250 fps[i]->unref();
251 }
bsalomon0b5b6b22015-10-14 08:31:34 -0700252 return !failed;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253}
254
robertphillips@google.comf294b772012-04-27 14:29:26 +0000255////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000256// sort out what kind of clip mask needs to be created: alpha, stencil,
257// scissor, or entirely software
joshualitt5e6ba212015-07-13 07:35:05 -0700258bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800259 GrPipelineBuilder::AutoRestoreStencil* ars,
bsalomon0ba8c242015-10-07 09:20:28 -0700260 const SkRect* devBounds,
261 GrAppliedClip* out) {
joshualitt7a6184f2014-10-29 18:29:27 -0700262 if (kRespectClip_StencilClipMode == fClipMode) {
263 fClipMode = kIgnoreClip_StencilClipMode;
264 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000265
tfarinabf54e492014-10-23 17:47:18 -0700266 GrReducedClip::ElementList elements(16);
brucedawson71d7f7f2015-02-26 13:28:53 -0800267 int32_t genID = 0;
268 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000269 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800270 bool requiresAA = false;
joshualitt5e6ba212015-07-13 07:35:05 -0700271 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000272
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273 // GrDrawTarget should have filtered this for us
bsalomon49f085d2014-09-05 13:34:00 -0700274 SkASSERT(rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000275
joshualitt44701df2015-02-23 14:44:57 -0800276 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
joshualitt5e6ba212015-07-13 07:35:05 -0700277 const GrClip& clip = pipelineBuilder.clip();
bsalomon96e02a82015-03-06 07:13:01 -0800278 if (clip.isWideOpen(clipSpaceRTIBounds)) {
egdaniel8dd688b2015-01-22 10:16:09 -0800279 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280 return true;
281 }
282
bsalomon96e02a82015-03-06 07:13:01 -0800283 // The clip mask manager always draws with a single IRect so we special case that logic here
284 // Image filters just use a rect, so we also special case that logic
285 switch (clip.clipType()) {
286 case GrClip::kWideOpen_ClipType:
287 SkFAIL("Should have caught this with clip.isWideOpen()");
288 return true;
bsalomon9ce30e12015-03-06 08:42:34 -0800289 case GrClip::kIRect_ClipType: {
290 SkIRect scissor = clip.irect();
291 if (scissor.intersect(clipSpaceRTIBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700292 out->fScissorState.set(scissor);
bsalomon9ce30e12015-03-06 08:42:34 -0800293 this->setPipelineBuilderStencil(pipelineBuilder, ars);
294 return true;
295 }
296 return false;
297 }
bsalomon96e02a82015-03-06 07:13:01 -0800298 case GrClip::kClipStack_ClipType: {
299 clipSpaceRTIBounds.offset(clip.origin());
300 GrReducedClip::ReduceClipStack(*clip.clipStack(),
301 clipSpaceRTIBounds,
302 &elements,
303 &genID,
304 &initialState,
305 &clipSpaceIBounds,
306 &requiresAA);
307 if (elements.isEmpty()) {
308 if (GrReducedClip::kAllIn_InitialState == initialState) {
309 if (clipSpaceIBounds == clipSpaceRTIBounds) {
310 this->setPipelineBuilderStencil(pipelineBuilder, ars);
311 return true;
312 }
313 } else {
314 return false;
315 }
316 }
317 } break;
318 }
319
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000320 // An element count of 4 was chosen because of the common pattern in Blink of:
321 // isect RR
322 // diff RR
323 // isect convex_poly
324 // isect convex_poly
325 // when drawing rounded div borders. This could probably be tuned based on a
326 // configuration's relative costs of switching RTs to generate a mask vs
327 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700328 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800329 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
330 SkIntToScalar(-clip.origin().fY) };
bsalomon0ba8c242015-10-07 09:20:28 -0700331 // When there are multiple color samples we want to do per-sample clipping, not compute
332 // a fractional pixel coverage.
333 bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled();
334 const GrFragmentProcessor* clipFP = nullptr;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000335 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700336 (requiresAA &&
337 this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
338 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700339 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800340 scissorSpaceIBounds.offset(-clip.origin());
halcanary96fcdcc2015-08-27 07:41:13 -0700341 if (nullptr == devBounds ||
mtklein217daa72014-07-02 12:55:21 -0700342 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bsalomone91f7b52015-10-27 06:42:50 -0700343 out->fScissorState.set(scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000344 }
egdaniel8dd688b2015-01-22 10:16:09 -0800345 this->setPipelineBuilderStencil(pipelineBuilder, ars);
bsalomon0ba8c242015-10-07 09:20:28 -0700346 out->fClipCoverageFP.reset(clipFP);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000347 return true;
348 }
349 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000350
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000351 // If MSAA is enabled we can do everything in the stencil buffer.
vbuzinov3e77ba92015-09-30 23:02:06 -0700352 if (0 == rt->numStencilSamples() && requiresAA) {
robertphillips588b9ca2015-10-04 08:40:31 -0700353 SkAutoTUnref<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354
joshualitt8059eb92014-12-29 15:10:07 -0800355 // The top-left of the mask corresponds to the top-left corner of the bounds.
356 SkVector clipToMaskOffset = {
357 SkIntToScalar(-clipSpaceIBounds.fLeft),
358 SkIntToScalar(-clipSpaceIBounds.fTop)
359 };
360
egdaniel8dd688b2015-01-22 10:16:09 -0800361 if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000362 // The clip geometry is complex enough that it will be more efficient to create it
363 // entirely in software
robertphillips588b9ca2015-10-04 08:40:31 -0700364 result.reset(this->createSoftwareClipMask(genID,
365 initialState,
366 elements,
367 clipToMaskOffset,
368 clipSpaceIBounds));
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000369 } else {
robertphillips588b9ca2015-10-04 08:40:31 -0700370 result.reset(this->createAlphaClipMask(genID,
371 initialState,
372 elements,
373 clipToMaskOffset,
374 clipSpaceIBounds));
robertphillips3f7357f2015-10-27 07:17:33 -0700375 // If createAlphaClipMask fails it means useSWOnlyPath has a bug
376 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000377 }
378
bsalomon49f085d2014-09-05 13:34:00 -0700379 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000380 // The mask's top left coord should be pinned to the rounded-out top left corner of
381 // clipSpace bounds. We determine the mask's position WRT to the render target here.
382 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800383 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon0ba8c242015-10-07 09:20:28 -0700384 out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds));
egdaniel8dd688b2015-01-22 10:16:09 -0800385 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 return true;
387 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000391 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800392 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
joshualitt9853cce2014-11-17 14:22:48 -0800393 this->createStencilClipMask(rt,
394 genID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000395 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396 elements,
397 clipSpaceIBounds,
398 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000399
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
401 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
402 // use both stencil and scissor test to the bounds for the final draw.
403 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
404 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
bsalomone91f7b52015-10-27 06:42:50 -0700405 out->fScissorState.set(scissorSpaceIBounds);
egdaniel8dd688b2015-01-22 10:16:09 -0800406 this->setPipelineBuilderStencil(pipelineBuilder, ars);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000407 return true;
408}
409
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000410namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800412// Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode
413void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage,
414 GrPipelineBuilder* pipelineBuilder) {
egdaniel87509242014-12-17 13:37:13 -0800415 SkASSERT(op <= SkRegion::kLastOp);
egdaniel8dd688b2015-01-22 10:16:09 -0800416 pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000418}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419
420////////////////////////////////////////////////////////////////////////////////
egdaniel8dd688b2015-01-22 10:16:09 -0800421bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800422 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800423 GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000424 const SkClipStack::Element* element,
425 GrPathRenderer* pr) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426
egdaniel8dd688b2015-01-22 10:16:09 -0800427 pipelineBuilder->setRenderTarget(target->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428
egdaniel87509242014-12-17 13:37:13 -0800429 // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP
430 // which ignores color.
431 GrColor color = GrColor_WHITE;
432
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000433 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000434 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000435 case Element::kEmpty_Type:
436 SkDEBUGFAIL("Should never get here with an empty element.");
437 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000438 case Element::kRect_Type:
joshualittb0a8a372014-09-23 09:50:21 -0700439 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
440 // the entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000441 if (element->isAA()) {
joshualitt8059eb92014-12-29 15:10:07 -0800442 SkRect devRect = element->getRect();
443 viewMatrix.mapRect(&devRect);
robertphillipsea461502015-05-26 11:38:03 -0700444
bsalomonb3b9aec2015-09-10 11:16:35 -0700445 fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix,
robertphillipsea461502015-05-26 11:38:03 -0700446 element->getRect(), devRect);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000447 } else {
bsalomonb3b9aec2015-09-10 11:16:35 -0700448 fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix,
joshualittd2b23e02015-08-21 10:53:34 -0700449 element->getRect());
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000450 }
451 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000452 default: {
453 SkPath path;
454 element->asPath(&path);
455 if (path.isInverseFillType()) {
456 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000457 }
kkinnunen18996512015-04-26 23:18:49 -0700458 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
halcanary96fcdcc2015-08-27 07:41:13 -0700459 if (nullptr == pr) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000460 GrPathRendererChain::DrawType type;
461 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
462 GrPathRendererChain::kColor_DrawType;
robertphillipscf10b5a2015-10-27 07:53:35 -0700463 pr = this->getContext()->getPathRenderer(*pipelineBuilder, viewMatrix,
egdaniel8dd688b2015-01-22 10:16:09 -0800464 path, stroke, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000465 }
halcanary96fcdcc2015-08-27 07:41:13 -0700466 if (nullptr == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 return false;
468 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700469 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700470 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700471 args.fResourceProvider = this->getContext()->resourceProvider();
472 args.fPipelineBuilder = pipelineBuilder;
473 args.fColor = color;
474 args.fViewMatrix = &viewMatrix;
475 args.fPath = &path;
476 args.fStroke = &stroke;
477 args.fAntiAlias = element->isAA();
478 pr->drawPath(args);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 break;
480 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481 }
482 return true;
483}
484
egdaniel8dd688b2015-01-22 10:16:09 -0800485void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800486 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000487 GrTexture* srcMask,
488 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000489 const SkIRect& dstBound,
490 const SkIRect& srcBound) {
egdaniel8dd688b2015-01-22 10:16:09 -0800491 pipelineBuilder->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492
egdaniel87509242014-12-17 13:37:13 -0800493 // We want to invert the coverage here
egdaniel8dd688b2015-01-22 10:16:09 -0800494 set_coverage_drawing_xpf(op, false, pipelineBuilder);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000495
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000496 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000497 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000498
bsalomonac856c92015-08-27 06:30:17 -0700499 pipelineBuilder->addCoverageFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700500 GrTextureDomainEffect::Create(srcMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000501 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000502 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
503 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000504 GrTextureParams::kNone_FilterMode))->unref();
joshualitt73bb4562015-03-25 07:16:21 -0700505
egdaniel87509242014-12-17 13:37:13 -0800506 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700507 fDrawTarget->drawNonAARect(*pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700508 GrColor_WHITE,
509 SkMatrix::I(),
510 SkRect::Make(dstBound));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511}
512
bsalomon427cf282014-10-16 13:41:43 -0700513GrTexture* GrClipMaskManager::createTempMask(int width, int height) {
bsalomonf2703d82014-10-28 14:33:06 -0700514 GrSurfaceDesc desc;
bsalomon3f490a02014-12-18 06:20:52 -0800515 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516 desc.fWidth = width;
517 desc.fHeight = height;
bsalomon76228632015-05-29 08:02:10 -0700518 if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
bsalomon51d1f7e2014-12-22 08:40:49 -0800519 desc.fConfig = kAlpha_8_GrPixelConfig;
520 } else {
521 desc.fConfig = kRGBA_8888_GrPixelConfig;
522 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000523
bsalomoneae62002015-07-31 13:59:30 -0700524 return this->getContext()->textureProvider()->createApproxTexture(desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000525}
526
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700528// Create a 8-bit clip mask in alpha
529
530static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
531 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
532 GrUniqueKey::Builder builder(key, kDomain, 3);
533 builder[0] = clipGenID;
534 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
535 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
536}
537
538GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key,
539 bool renderTarget) {
540 GrSurfaceDesc desc;
541 desc.fWidth = width;
542 desc.fHeight = height;
543 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
544 if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
545 desc.fConfig = kAlpha_8_GrPixelConfig;
546 } else {
547 desc.fConfig = kRGBA_8888_GrPixelConfig;
548 }
549
550 GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0);
551 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700552 return nullptr;
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000553 }
bsalomon473addf2015-10-02 07:49:05 -0700554 texture->resourcePriv().setUniqueKey(key);
555 return texture;
krajcevskiad1dc582014-06-10 15:06:47 -0700556}
557
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000558GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700559 GrReducedClip::InitialState initialState,
560 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800561 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700563 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
564 GrUniqueKey key;
565 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
566 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
bsalomon473addf2015-10-02 07:49:05 -0700567 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000568 }
569
bsalomon473addf2015-10-02 07:49:05 -0700570 SkAutoTUnref<GrTexture> texture(this->createCachedMask(
571 clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true));
572
krajcevskiad1dc582014-06-10 15:06:47 -0700573 // There's no texture in the cache. Let's try to allocate it then.
bsalomon473addf2015-10-02 07:49:05 -0700574 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700575 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576 }
577
joshualitt8059eb92014-12-29 15:10:07 -0800578 // Set the matrix so that rendered clip elements are transformed to mask space from clip
579 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700580 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800581
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000582 // The texture may be larger than necessary, this rect represents the part of the texture
583 // we populate with a rasterization of the clip.
584 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
585
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000586 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
587 // clear the part that we care about.
bsalomonb3b9aec2015-09-10 11:16:35 -0700588 fDrawTarget->clear(&maskSpaceIBounds,
joshualitt329bf482014-10-29 12:31:28 -0700589 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
590 true,
bsalomon473addf2015-10-02 07:49:05 -0700591 texture->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000592
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000593 // When we use the stencil in the below loop it is important to have this clip installed.
594 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
595 // pass must not set values outside of this bounds or stencil values outside the rect won't be
596 // cleared.
joshualitt44701df2015-02-23 14:44:57 -0800597 GrClip clip(maskSpaceIBounds);
bsalomon427cf282014-10-16 13:41:43 -0700598 SkAutoTUnref<GrTexture> temp;
joshualitt9853cce2014-11-17 14:22:48 -0800599
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700601 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000602 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000603 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000604 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000605 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
egdaniel8dd688b2015-01-22 10:16:09 -0800606 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800607
joshualitt44701df2015-02-23 14:44:57 -0800608 pipelineBuilder.setClip(clip);
halcanary96fcdcc2015-08-27 07:41:13 -0700609 GrPathRenderer* pr = nullptr;
robertphillips423e3372015-10-27 09:23:38 -0700610 bool useTemp = !can_stencil_and_draw_element(this->getContext(), &pipelineBuilder,
611 texture, translate, element, &pr);
612
613 // useSWOnlyPath should now filter out all cases where gpu-side mask merging is
614 // performed. See skbug.com/4519 for rationale and details.
615 SkASSERT(!useTemp);
616
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000618 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000619 // mask buffer can be substantially larger than the actually clip stack element. We
620 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000621 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000622 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000623
624 if (useTemp) {
625 if (invert) {
626 maskSpaceElementIBounds = maskSpaceIBounds;
627 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000628 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000629 elementBounds.offset(clipToMaskOffset);
630 elementBounds.roundOut(&maskSpaceElementIBounds);
631 }
632
bsalomon427cf282014-10-16 13:41:43 -0700633 if (!temp) {
634 temp.reset(this->createTempMask(maskSpaceIBounds.fRight,
635 maskSpaceIBounds.fBottom));
636 if (!temp) {
bsalomon473addf2015-10-02 07:49:05 -0700637 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700638 return nullptr;
bsalomon427cf282014-10-16 13:41:43 -0700639 }
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000640 }
bsalomon427cf282014-10-16 13:41:43 -0700641 dst = temp;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000642 // clear the temp target and set blend to replace
bsalomonb3b9aec2015-09-10 11:16:35 -0700643 fDrawTarget->clear(&maskSpaceElementIBounds,
joshualitt9853cce2014-11-17 14:22:48 -0800644 invert ? 0xffffffff : 0x00000000,
645 true,
646 dst->asRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -0800647 set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000648 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000649 // draw directly into the result with the stencil set to make the pixels affected
650 // by the clip shape be non-zero.
bsalomon473addf2015-10-02 07:49:05 -0700651 dst = texture;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000652 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
653 kReplace_StencilOp,
654 kReplace_StencilOp,
655 kAlways_StencilFunc,
656 0xffff,
657 0xffff,
658 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800659 pipelineBuilder.setStencil(kStencilInElement);
660 set_coverage_drawing_xpf(op, invert, &pipelineBuilder);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000661 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000662
egdaniel8dd688b2015-01-22 10:16:09 -0800663 if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) {
bsalomon473addf2015-10-02 07:49:05 -0700664 texture->resourcePriv().removeUniqueKey();
halcanary96fcdcc2015-08-27 07:41:13 -0700665 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000666 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000668 if (useTemp) {
egdaniel8dd688b2015-01-22 10:16:09 -0800669 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700670 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800671
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000672 // Now draw into the accumulator using the real operation and the temp buffer as a
673 // texture
egdaniel8dd688b2015-01-22 10:16:09 -0800674 this->mergeMask(&backgroundPipelineBuilder,
bsalomon473addf2015-10-02 07:49:05 -0700675 texture,
bsalomon427cf282014-10-16 13:41:43 -0700676 temp,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000677 op,
678 maskSpaceIBounds,
679 maskSpaceElementIBounds);
680 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800681 GrPipelineBuilder backgroundPipelineBuilder;
bsalomon473addf2015-10-02 07:49:05 -0700682 backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800683
egdaniel8dd688b2015-01-22 10:16:09 -0800684 set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000685 // Draw to the exterior pixels (those with a zero stencil value).
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000686 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
687 kZero_StencilOp,
688 kZero_StencilOp,
689 kEqual_StencilFunc,
690 0xffff,
691 0x0000,
692 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800693 backgroundPipelineBuilder.setStencil(kDrawOutsideElement);
joshualitt73bb4562015-03-25 07:16:21 -0700694
egdaniel87509242014-12-17 13:37:13 -0800695 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomonb3b9aec2015-09-10 11:16:35 -0700696 fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate,
joshualittd2b23e02015-08-21 10:53:34 -0700697 clipSpaceIBounds);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000698 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800700 GrPipelineBuilder pipelineBuilder;
joshualitt9853cce2014-11-17 14:22:48 -0800701
robertphillips@google.come79f3202014-02-11 16:30:21 +0000702 // all the remaining ops can just be directly draw into the accumulation buffer
egdaniel8dd688b2015-01-22 10:16:09 -0800703 set_coverage_drawing_xpf(op, false, &pipelineBuilder);
egdaniel87509242014-12-17 13:37:13 -0800704 // The color passed in here does not matter since the coverageSetOpXP won't read it.
bsalomon473addf2015-10-02 07:49:05 -0700705 this->drawElement(&pipelineBuilder, translate, texture, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000706 }
707 }
708
bsalomon473addf2015-10-02 07:49:05 -0700709 return texture.detach();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000710}
711
712////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000713// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000714// (as opposed to canvas) coordinates
joshualitt9853cce2014-11-17 14:22:48 -0800715bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt,
716 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700717 GrReducedClip::InitialState initialState,
718 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000719 const SkIRect& clipSpaceIBounds,
720 const SkIPoint& clipSpaceToStencilOffset) {
bsalomon49f085d2014-09-05 13:34:00 -0700721 SkASSERT(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
egdanielec00d942015-09-14 12:56:10 -0700723 GrStencilAttachment* stencilAttachment =
724 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
halcanary96fcdcc2015-08-27 07:41:13 -0700725 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 return false;
727 }
728
egdaniel8dc7c3a2015-04-16 11:22:42 -0700729 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
730 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000731 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
732 SkVector translate = {
733 SkIntToScalar(clipSpaceToStencilOffset.fX),
734 SkIntToScalar(clipSpaceToStencilOffset.fY)
735 };
joshualitt8059eb92014-12-29 15:10:07 -0800736 SkMatrix viewMatrix;
737 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738
bsalomon@google.com9f131742012-12-13 20:43:56 +0000739 // We set the current clip to the bounds so that our recursive draws are scissored to them.
740 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
741 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
joshualitt44701df2015-02-23 14:44:57 -0800742 GrClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
egdaniel8dc7c3a2015-04-16 11:22:42 -0700744 int clipBit = stencilAttachment->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000745 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746 clipBit = (1 << (clipBit-1));
747
bsalomonb3b9aec2015-09-10 11:16:35 -0700748 fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds,
749 GrReducedClip::kAllIn_InitialState == initialState, rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750
751 // walk through each clip element and perform its set op
752 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700753 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000754 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800755
egdaniel8dd688b2015-01-22 10:16:09 -0800756 GrPipelineBuilder pipelineBuilder;
joshualitt44701df2015-02-23 14:44:57 -0800757 pipelineBuilder.setClip(clip);
egdaniel8dd688b2015-01-22 10:16:09 -0800758 pipelineBuilder.setRenderTarget(rt);
egdaniel080e6732014-12-22 07:35:52 -0800759
egdaniel8dd688b2015-01-22 10:16:09 -0800760 pipelineBuilder.setDisableColorXPFactory();
joshualitt9853cce2014-11-17 14:22:48 -0800761
762 // if the target is MSAA then we want MSAA enabled when the clip is soft
vbuzinov3e77ba92015-09-30 23:02:06 -0700763 if (rt->isStencilBufferMultisampled()) {
bsalomond79c5492015-04-27 10:07:04 -0700764 pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA());
joshualitt9853cce2014-11-17 14:22:48 -0800765 }
766
tomhudson@google.com8afae612012-08-14 15:03:35 +0000767 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 // enabled at bottom of loop
joshualitt7a6184f2014-10-29 18:29:27 -0700769 fClipMode = kIgnoreClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000771 // This will be used to determine whether the clip shape can be rendered into the
772 // stencil with arbitrary stencil settings.
773 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774
kkinnunen18996512015-04-26 23:18:49 -0700775 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000776 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000777
halcanary96fcdcc2015-08-27 07:41:13 -0700778 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000779 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000780 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000781 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000783 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000784 element->asPath(&clipPath);
785 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000786 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000787 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000788 }
robertphillipscf10b5a2015-10-27 07:53:35 -0700789 pr = this->getContext()->getPathRenderer(pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800790 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800791 clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000792 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000793 false,
794 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000795 &stencilSupport);
halcanary96fcdcc2015-08-27 07:41:13 -0700796 if (nullptr == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 return false;
798 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 }
800
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801 int passes;
802 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
803
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000804 bool canRenderDirectToStencil =
805 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000807 // fill rule, and set operation can
808 // we render the element directly to
809 // stencil bit used for clipping.
810 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
811 canRenderDirectToStencil,
812 clipBit,
813 fillInverted,
814 &passes,
815 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816
817 // draw the element to the client stencil bits if necessary
818 if (!canDrawDirectToClip) {
819 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000820 kIncClamp_StencilOp,
821 kIncClamp_StencilOp,
822 kAlways_StencilFunc,
823 0xffff,
824 0x0000,
825 0xffff);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000826 if (Element::kRect_Type == element->getType()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800827 *pipelineBuilder.stencil() = gDrawToStencil;
joshualitt73bb4562015-03-25 07:16:21 -0700828
829 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700830 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700831 GrColor_WHITE,
832 viewMatrix,
833 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000835 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000836 if (canRenderDirectToStencil) {
egdaniel8dd688b2015-01-22 10:16:09 -0800837 *pipelineBuilder.stencil() = gDrawToStencil;
bsalomon0aff2fa2015-07-31 06:48:27 -0700838
839 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700840 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700841 args.fResourceProvider = this->getContext()->resourceProvider();
842 args.fPipelineBuilder = &pipelineBuilder;
843 args.fColor = GrColor_WHITE;
844 args.fViewMatrix = &viewMatrix;
845 args.fPath = &clipPath;
846 args.fStroke = &stroke;
847 args.fAntiAlias = false;
848 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000849 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700850 GrPathRenderer::StencilPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700851 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700852 args.fResourceProvider = this->getContext()->resourceProvider();
853 args.fPipelineBuilder = &pipelineBuilder;
854 args.fViewMatrix = &viewMatrix;
855 args.fPath = &clipPath;
856 args.fStroke = &stroke;
857 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000858 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 }
860 }
861 }
862
863 // now we modify the clip bit by rendering either the clip
864 // element directly or a bounding rect of the entire clip.
joshualitt7a6184f2014-10-29 18:29:27 -0700865 fClipMode = kModifyClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000866 for (int p = 0; p < passes; ++p) {
joshualitt4f6dc522015-07-09 12:17:44 -0700867 *pipelineBuilder.stencil() = stencilSettings[p];
joshualitt9853cce2014-11-17 14:22:48 -0800868
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000870 if (Element::kRect_Type == element->getType()) {
joshualitt73bb4562015-03-25 07:16:21 -0700871 // We need this AGP until everything is in GrBatch
bsalomonb3b9aec2015-09-10 11:16:35 -0700872 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700873 GrColor_WHITE,
874 viewMatrix,
875 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700877 GrPathRenderer::DrawPathArgs args;
bsalomonb3b9aec2015-09-10 11:16:35 -0700878 args.fTarget = fDrawTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700879 args.fResourceProvider = this->getContext()->resourceProvider();
880 args.fPipelineBuilder = &pipelineBuilder;
881 args.fColor = GrColor_WHITE;
882 args.fViewMatrix = &viewMatrix;
883 args.fPath = &clipPath;
884 args.fStroke = &stroke;
885 args.fAntiAlias = false;
886 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000887 }
888 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000889 // The view matrix is setup to do clip space -> stencil space translation, so
890 // draw rect in clip space.
bsalomonb3b9aec2015-09-10 11:16:35 -0700891 fDrawTarget->drawNonAARect(pipelineBuilder,
joshualittd2b23e02015-08-21 10:53:34 -0700892 GrColor_WHITE,
893 viewMatrix,
894 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 }
896 }
897 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000898 }
joshualitt7a6184f2014-10-29 18:29:27 -0700899 fClipMode = kRespectClip_StencilClipMode;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000900 return true;
901}
902
bsalomon@google.com411dad02012-06-05 20:24:20 +0000903// mapping of clip-respecting stencil funcs to normal stencil funcs
904// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000905static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
907 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
908 // In the Clip Funcs
909 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
910 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
911 kLess_StencilFunc, // kLessIfInClip_StencilFunc
912 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
913 // Special in the clip func that forces user's ref to be 0.
914 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
915 // make ref 0 and do normal nequal.
916 },
917 {// Stencil-Clipping is ENABLED
918 // In the Clip Funcs
919 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
920 // eq stencil clip bit, mask
921 // out user bits.
922
923 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
924 // add stencil bit to mask and ref
925
926 kLess_StencilFunc, // kLessIfInClip_StencilFunc
927 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
928 // for both of these we can add
929 // the clip bit to the mask and
930 // ref and compare as normal
931 // Special in the clip func that forces user's ref to be 0.
932 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
933 // make ref have only the clip bit set
934 // and make comparison be less
935 // 10..0 < 1..user_bits..
936 }
937};
938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939namespace {
940// Sets the settings to clip against the stencil buffer clip while ignoring the
941// client bits.
942const GrStencilSettings& basic_apply_stencil_clip_settings() {
943 // stencil settings to use when clip is in stencil
944 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
945 kKeep_StencilOp,
946 kKeep_StencilOp,
947 kAlwaysIfInClip_StencilFunc,
948 0x0000,
949 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000950 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
952}
953}
954
joshualitt5e6ba212015-07-13 07:35:05 -0700955void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder,
egdaniel8dd688b2015-01-22 10:16:09 -0800956 GrPipelineBuilder::AutoRestoreStencil* ars) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 // We make two copies of the StencilSettings here (except in the early
958 // exit scenario. One copy from draw state to the stack var. Then another
959 // from the stack var to the gpu. We could make this class hold a ptr to
960 // GrGpu's fStencilSettings and eliminate the stack copy here.
961
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962 // use stencil for clipping if clipping is enabled and the clip
963 // has been written into the stencil.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000964 GrStencilSettings settings;
joshualitt9853cce2014-11-17 14:22:48 -0800965
bsalomon@google.coma3201942012-06-21 19:58:20 +0000966 // The GrGpu client may not be using the stencil buffer but we may need to
967 // enable it in order to respect a stencil clip.
joshualitt5e6ba212015-07-13 07:35:05 -0700968 if (pipelineBuilder.getStencil().isDisabled()) {
joshualitt7a6184f2014-10-29 18:29:27 -0700969 if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 settings = basic_apply_stencil_clip_settings();
971 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000972 return;
973 }
974 } else {
joshualitt5e6ba212015-07-13 07:35:05 -0700975 settings = pipelineBuilder.getStencil();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 }
977
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 int stencilBits = 0;
joshualitt5e6ba212015-07-13 07:35:05 -0700979 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700980 GrStencilAttachment* stencilAttachment =
981 fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700982 if (stencilAttachment) {
983 stencilBits = stencilAttachment->bits();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000984 }
985
bsalomonb3b9aec2015-09-10 11:16:35 -0700986 SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
987 SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
joshualitt7a6184f2014-10-29 18:29:27 -0700988 this->adjustStencilParams(&settings, fClipMode, stencilBits);
joshualitt5e6ba212015-07-13 07:35:05 -0700989 ars->set(&pipelineBuilder);
990 ars->setStencil(settings);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991}
992
993void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
994 StencilClipMode mode,
995 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000996 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000997
998 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 // We assume that this clip manager itself is drawing to the GrGpu and
1000 // has already setup the correct values.
1001 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001002 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003
bsalomon@google.com411dad02012-06-05 20:24:20 +00001004 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1005 unsigned int userBits = clipBit - 1;
1006
bsalomon@google.coma3201942012-06-21 19:58:20 +00001007 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomonb3b9aec2015-09-10 11:16:35 -07001008 bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001009
bsalomon@google.coma3201942012-06-21 19:58:20 +00001010 bool finished = false;
1011 while (!finished) {
1012 GrStencilFunc func = settings->func(face);
1013 uint16_t writeMask = settings->writeMask(face);
1014 uint16_t funcMask = settings->funcMask(face);
1015 uint16_t funcRef = settings->funcRef(face);
1016
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001017 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018
1019 writeMask &= userBits;
1020
1021 if (func >= kBasicStencilFuncCount) {
1022 int respectClip = kRespectClip_StencilClipMode == mode;
1023 if (respectClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 switch (func) {
1025 case kAlwaysIfInClip_StencilFunc:
1026 funcMask = clipBit;
1027 funcRef = clipBit;
1028 break;
1029 case kEqualIfInClip_StencilFunc:
1030 case kLessIfInClip_StencilFunc:
1031 case kLEqualIfInClip_StencilFunc:
1032 funcMask = (funcMask & userBits) | clipBit;
1033 funcRef = (funcRef & userBits) | clipBit;
1034 break;
1035 case kNonZeroIfInClip_StencilFunc:
1036 funcMask = (funcMask & userBits) | clipBit;
1037 funcRef = clipBit;
1038 break;
1039 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001040 SkFAIL("Unknown stencil func");
bsalomon@google.coma3201942012-06-21 19:58:20 +00001041 }
1042 } else {
1043 funcMask &= userBits;
1044 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001045 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001046 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001047 gSpecialToBasicStencilFunc[respectClip];
1048 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001049 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001050 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001051 funcMask &= userBits;
1052 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001053 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001054
1055 settings->setFunc(face, func);
1056 settings->setWriteMask(face, writeMask);
1057 settings->setFuncMask(face, funcMask);
1058 settings->setFuncRef(face, funcRef);
1059
1060 if (GrStencilSettings::kFront_Face == face) {
1061 face = GrStencilSettings::kBack_Face;
1062 finished = !twoSided;
1063 } else {
1064 finished = true;
1065 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001066 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001067 if (!twoSided) {
1068 settings->copyFrontSettingsToBack();
1069 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070}
1071
1072////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +00001073GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001074 GrReducedClip::InitialState initialState,
1075 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -08001076 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001077 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -07001078 GrUniqueKey key;
1079 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
1080 GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider();
1081 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
1082 return texture;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001083 }
1084
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001085 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1086 // the top left corner of the resulting rect to the top left of the texture.
1087 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1088
robertphillips@google.com2c756812012-05-22 20:28:23 +00001089 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001090
joshualitt8059eb92014-12-29 15:10:07 -08001091 // Set the matrix so that rendered clip elements are transformed to mask space from clip
1092 // space.
1093 SkMatrix translate;
1094 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -08001095
joshualitt8059eb92014-12-29 15:10:07 -08001096 helper.init(maskSpaceIBounds, &translate, false);
tfarinabf54e492014-10-23 17:47:18 -07001097 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001098 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001099
tfarinabf54e492014-10-23 17:47:18 -07001100 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001101 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001102 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001103
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001104 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1105 // Intersect and reverse difference require modifying pixels outside of the geometry
1106 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1107 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1108 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001109 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001110 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001111 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001112 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001114 SkPath clipPath;
1115 element->asPath(&clipPath);
1116 clipPath.toggleInverseFillType();
1117 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 continue;
1119 }
1120
1121 // The other ops (union, xor, diff) only affect pixels inside
1122 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001123 if (Element::kRect_Type == element->getType()) {
1124 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1125 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001126 SkPath path;
1127 element->asPath(&path);
1128 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001129 }
1130 }
1131
krajcevskiad1dc582014-06-10 15:06:47 -07001132 // Allocate clip mask texture
bsalomon473addf2015-10-02 07:49:05 -07001133 GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(),
1134 key, false);
halcanary96fcdcc2015-08-27 07:41:13 -07001135 if (nullptr == result) {
halcanary96fcdcc2015-08-27 07:41:13 -07001136 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -07001137 }
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001138 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001139
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001140 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001141}
1142
robertphillips@google.comf294b772012-04-27 14:29:26 +00001143////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001144
egdaniel8dc7c3a2015-04-16 11:22:42 -07001145void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment,
joshualitt9853cce2014-11-17 14:22:48 -08001146 GrStencilSettings* settings) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001147 if (stencilAttachment) {
1148 int stencilBits = stencilAttachment->bits();
joshualitt7a6184f2014-10-29 18:29:27 -07001149 this->adjustStencilParams(settings, fClipMode, stencilBits);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001150 }
1151}