blob: 8233786ba2d1686edeedcc4955fdc73f38c7c2cb [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
csmartdaltonc6f411e2016-08-05 22:32:12 -07008#include "GrClipStackClip.h"
9
csmartdalton28341fa2016-08-17 10:00:21 -070010#include "GrAppliedClip.h"
robertphillips68737822015-10-29 12:12:21 -070011#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080012#include "GrDrawContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070014#include "GrGpuResourcePriv.h"
csmartdalton28341fa2016-08-17 10:00:21 -070015#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070016#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080018#include "effects/GrConvexPolyEffect.h"
19#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080020#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000021
bsalomon@google.com8182fa02012-12-04 14:06:06 +000022typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070023typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070024typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000025
robertphillips976f5f02016-06-03 10:59:20 -070026static const int kMaxAnalyticElements = 4;
27
csmartdaltonc6f411e2016-08-05 22:32:12 -070028bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070029 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070030 return true;
31 }
32 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
33 SkIntToScalar(fOrigin.y())));
34}
35
bsalomon7f0d9f32016-08-15 14:49:10 -070036bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070037 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070038 return true;
39 }
40 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
41 SkIntToScalar(fOrigin.fY)));
42}
43
bsalomoncb31e512016-08-26 10:48:19 -070044bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, bool* aa) const {
45 if (!fStack) {
46 return false;
47 }
48 const SkRect* rtBounds = &origRTBounds;
49 SkRect tempRTBounds;
50 bool origin = fOrigin.fX || fOrigin.fY;
51 if (origin) {
52 tempRTBounds = origRTBounds;
53 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
54 rtBounds = &tempRTBounds;
55 }
56 if (fStack->isRRect(*rtBounds, rr, aa)) {
57 if (origin) {
58 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
59 }
60 return true;
61 }
62 return false;
63}
64
csmartdaltonc6f411e2016-08-05 22:32:12 -070065void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
66 bool* isIntersectionOfRects) const {
67 if (!fStack) {
68 devResult->setXYWH(0, 0, width, height);
69 if (isIntersectionOfRects) {
70 *isIntersectionOfRects = true;
71 }
72 return;
73 }
74 SkRect devBounds;
75 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
76 isIntersectionOfRects);
77 devBounds.roundOut(devResult);
78}
79
bsalomon@google.com51a62862012-11-26 21:19:43 +000080////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000082// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070083static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
84 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000085 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080086 // We use device coords to compute the texture coordinates. We set our matrix to be a
87 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000088 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000091
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000092 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070093 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070094 result,
brianosman54f30c12016-07-18 10:53:52 -070095 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070096 mat,
97 GrTextureDomain::MakeTexelDomain(result, domainTexels),
98 GrTextureDomain::kDecal_Mode,
99 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -0700100 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000101}
102
robertphillips3f7357f2015-10-27 07:17:33 -0700103// Does the path in 'element' require SW rendering? If so, return true (and,
104// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
105// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -0700106bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
107 bool hasUserStencilSettings,
108 const GrDrawContext* drawContext,
109 const SkMatrix& viewMatrix,
110 const Element* element,
111 GrPathRenderer** prOut,
112 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700113 if (Element::kRect_Type == element->getType()) {
114 // rects can always be drawn directly w/o using the software path
115 // TODO: skip rrects once we're drawing them directly.
116 if (prOut) {
117 *prOut = nullptr;
118 }
119 return false;
120 } else {
121 // We shouldn't get here with an empty clip element.
122 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700123
robertphillips3f7357f2015-10-27 07:17:33 -0700124 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
125 SkPath path;
126 element->asPath(&path);
127 if (path.isInverseFillType()) {
128 path.toggleInverseFillType();
129 }
halcanary9d524f22016-03-29 09:03:52 -0700130
robertphillips3f7357f2015-10-27 07:17:33 -0700131 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700132
robertphillips423e3372015-10-27 09:23:38 -0700133 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700134 type = element->isAA()
135 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
136 : GrPathRendererChain::kStencilAndColor_DrawType;
137 } else {
138 type = element->isAA()
139 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700140 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700141 }
halcanary9d524f22016-03-29 09:03:52 -0700142
bsalomon8acedde2016-06-24 10:42:16 -0700143 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700144 GrPathRenderer::CanDrawPathArgs canDrawArgs;
145 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
146 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700147 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700148 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700149 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700150 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700151
robertphillips3f7357f2015-10-27 07:17:33 -0700152 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700153 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700154 if (prOut) {
155 *prOut = pr;
156 }
157 return SkToBool(!pr);
158 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000159}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000160
robertphillips@google.comfa662942012-05-17 12:20:22 +0000161/*
162 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
163 * will be used on any element. If so, it returns true to indicate that the
164 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
165 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700166bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
167 bool hasUserStencilSettings,
168 const GrDrawContext* drawContext,
169 const SkVector& clipToMaskOffset,
csmartdalton77f2fae2016-08-08 09:55:06 -0700170 const ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000171 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000172 // a clip gets complex enough it can just be done in SW regardless
173 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000174
joshualitt8059eb92014-12-29 15:10:07 -0800175 // Set the matrix so that rendered clip elements are transformed to mask space from clip
176 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700177 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800178
csmartdalton77f2fae2016-08-08 09:55:06 -0700179 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000180 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700181
182 SkRegion::Op op = element->getOp();
183 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700184 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700185 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700186
robertphillips59cf61a2016-07-13 09:18:21 -0700187 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700188 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700189 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000190 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000191 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000192 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000193}
194
csmartdalton77f2fae2016-08-08 09:55:06 -0700195static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700196 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700197 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700198 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700199 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000200 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700201 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700202 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700203 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700204 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700205 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 SkRegion::Op op = iter.get()->getOp();
207 bool invert;
208 bool skip = false;
209 switch (op) {
210 case SkRegion::kReplace_Op:
211 SkASSERT(iter.get() == elements.head());
212 // Fallthrough, handled same as intersect.
213 case SkRegion::kIntersect_Op:
214 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700215 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 skip = true;
217 }
218 break;
219 case SkRegion::kDifference_Op:
220 invert = true;
221 // We don't currently have a cheap test for whether a rect is fully outside an
222 // element's primitive, so don't attempt to set skip.
223 break;
224 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700225 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000226 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000227 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700228 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800229 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700230 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700231 return false;
bsalomona912dde2015-10-14 15:01:50 -0700232 }
joshualittb0a8a372014-09-23 09:50:21 -0700233 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700234 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700236 edgeType =
237 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 }
bsalomona912dde2015-10-14 15:01:50 -0700239
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 switch (iter.get()->getType()) {
241 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700242 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
243 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000244 break;
245 case SkClipStack::Element::kRRect_Type: {
246 SkRRect rrect = iter.get()->getRRect();
247 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700248 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000249 break;
250 }
251 case SkClipStack::Element::kRect_Type: {
252 SkRect rect = iter.get()->getRect();
253 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700254 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000255 break;
256 }
257 default:
258 break;
259 }
bungeman06ca8ec2016-06-09 08:01:03 -0700260 if (!fps.back()) {
261 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000262 }
263 }
mtklein217daa72014-07-02 12:55:21 -0700264 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000265 }
266
bsalomon0b5b6b22015-10-14 08:31:34 -0700267 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700268 if (fps.count()) {
269 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270 }
bungeman06ca8ec2016-06-09 08:01:03 -0700271 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272}
273
robertphillips@google.comf294b772012-04-27 14:29:26 +0000274////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000275// sort out what kind of clip mask needs to be created: alpha, stencil,
276// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700277bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
278 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700279 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700280 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700281 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000282
csmartdaltoncbecb082016-07-22 08:59:08 -0700283 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700284 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700285 return false;
286 }
287
csmartdaltonc6f411e2016-08-05 22:32:12 -0700288 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
289 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700290
csmartdalton77f2fae2016-08-08 09:55:06 -0700291 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
292 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293
csmartdaltond211e782016-08-15 11:17:19 -0700294 if (reducedClip.hasIBounds() &&
295 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
296 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
297 scissorSpaceIBounds.offset(-fOrigin);
298 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700299 }
cdalton93a379b2016-05-11 13:58:08 -0700300
csmartdaltond211e782016-08-15 11:17:19 -0700301 if (reducedClip.elements().isEmpty()) {
302 return InitialState::kAllIn == reducedClip.initialState();
303 }
304
305 SkASSERT(reducedClip.hasIBounds());
306
csmartdalton28341fa2016-08-17 10:00:21 -0700307 // Attempt to implement difference clip rects with window rectangles. This will eventually
308 // become more comprehensive.
309 if (drawContext->accessRenderTarget()->renderTargetPriv().supportsWindowRectangles() &&
310 1 == reducedClip.elements().count() && !reducedClip.requiresAA() &&
311 InitialState::kAllIn == reducedClip.initialState()) {
312 const Element* element = reducedClip.elements().head();
313 SkRegion::Op op = element->getOp();
314 if (Element::kRect_Type == element->getType() &&
315 (SkRegion::kDifference_Op == op || SkRegion::kXOR_Op == op)) {
316 SkIRect window;
317 element->getRect().round(&window);
318 window.offset(-fOrigin);
319 out->addWindowRectangle(window);
320 return true;
321 }
322 }
323
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000324 // An element count of 4 was chosen because of the common pattern in Blink of:
325 // isect RR
326 // diff RR
327 // isect convex_poly
328 // isect convex_poly
329 // when drawing rounded div borders. This could probably be tuned based on a
330 // configuration's relative costs of switching RTs to generate a mask vs
331 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700332 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800333 // When there are multiple samples we want to do per-sample clipping, not compute a
334 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700335 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
336 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700337 // With a single color sample, any coverage info is lost from color once it hits the
338 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
339 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700340 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700341 }
bungeman06ca8ec2016-06-09 08:01:03 -0700342 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700343 if (reducedClip.requiresAA() &&
344 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
345 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700346 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000347 return true;
348 }
349 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000350
cdaltonede75742015-11-11 15:27:57 -0800351 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700352 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700353 sk_sp<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 = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700357 SkIntToScalar(-reducedClip.left()),
358 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800359 };
360
robertphillips59cf61a2016-07-13 09:18:21 -0700361 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700362 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363 // The clip geometry is complex enough that it will be more efficient to create it
364 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700365 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
366 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700368 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800369 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700370 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371 }
372
bsalomon49f085d2014-09-05 13:34:00 -0700373 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374 // The mask's top left coord should be pinned to the rounded-out top left corner of
375 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700376 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700377 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700378 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 return true;
380 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000381 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000384 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700385 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700386 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
csmartdaltond211e782016-08-15 11:17:19 -0700387 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000388 return true;
389}
390
robertphillips391395d2016-03-02 09:26:36 -0800391static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700392 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700393 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800394 const SkMatrix& viewMatrix,
395 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800396
397 // TODO: Draw rrects directly here.
398 switch (element->getType()) {
399 case Element::kEmpty_Type:
400 SkDEBUGFAIL("Should never get here with an empty element.");
401 break;
robertphillips391395d2016-03-02 09:26:36 -0800402 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700403 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800404 element->getOp(),
405 element->isInverseFilled(),
406 element->isAA(),
407 viewMatrix, element->getRect());
408 break;
robertphillips86c60752016-03-02 08:43:13 -0800409 default: {
410 SkPath path;
411 element->asPath(&path);
412 if (path.isInverseFillType()) {
413 path.toggleInverseFillType();
414 }
415
cdalton862cff32016-05-12 15:09:48 -0700416 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800417 element->getOp(),
418 element->isInverseFilled(),
419 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800420 break;
421 }
422 }
robertphillips391395d2016-03-02 09:26:36 -0800423
424 return false;
425}
426
427static void draw_element(GrDrawContext* dc,
428 const GrClip& clip, // TODO: can this just always be WideOpen?
429 const GrPaint &paint,
430 const SkMatrix& viewMatrix,
431 const SkClipStack::Element* element) {
432
433 // TODO: Draw rrects directly here.
434 switch (element->getType()) {
435 case Element::kEmpty_Type:
436 SkDEBUGFAIL("Should never get here with an empty element.");
437 break;
438 case Element::kRect_Type:
439 dc->drawRect(clip, paint, viewMatrix, element->getRect());
440 break;
441 default: {
442 SkPath path;
443 element->asPath(&path);
444 if (path.isInverseFillType()) {
445 path.toggleInverseFillType();
446 }
447
bsalomon6663acf2016-05-10 09:14:17 -0700448 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800449 break;
450 }
451 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452}
453
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000454////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700455// Create a 8-bit clip mask in alpha
456
457static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
458 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
459 GrUniqueKey::Builder builder(key, kDomain, 3);
460 builder[0] = clipGenID;
461 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
462 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
463}
464
csmartdaltonc6f411e2016-08-05 22:32:12 -0700465sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700466 const GrReducedClip& reducedClip,
467 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800468 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700469 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700470 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700471 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700472 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000473 }
474
robertphillips544b9aa2015-10-28 11:01:41 -0700475 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700476 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800477 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700478 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800479 }
480
robertphillips6738c702016-07-27 12:13:51 -0700481 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700482 reducedClip.width(),
483 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700484 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800485 if (!dc) {
486 return nullptr;
487 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700488
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 // The texture may be larger than necessary, this rect represents the part of the texture
490 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700491 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000493 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
494 // clear the part that we care about.
csmartdaltond211e782016-08-15 11:17:19 -0700495 dc->clear(&maskSpaceIBounds, InitialState::kAllIn == reducedClip.initialState() ? -1 : 0, true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000496
robertphillips391395d2016-03-02 09:26:36 -0800497 // Set the matrix so that rendered clip elements are transformed to mask space from clip
498 // space.
499 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
500
501 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000502 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
503 // pass must not set values outside of this bounds or stencil values outside the rect won't be
504 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800505
robertphillips@google.comf294b772012-04-27 14:29:26 +0000506 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700507 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000509 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000510 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000511 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700512 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700513
robertphillips391395d2016-03-02 09:26:36 -0800514 // draw directly into the result with the stencil set to make the pixels affected
515 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700516 static constexpr GrUserStencilSettings kStencilInElement(
517 GrUserStencilSettings::StaticInit<
518 0xffff,
519 GrUserStencilTest::kAlways,
520 0xffff,
521 GrUserStencilOp::kReplace,
522 GrUserStencilOp::kReplace,
523 0xffff>()
524 );
cdalton862cff32016-05-12 15:09:48 -0700525 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800526 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800527 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000528 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000529
robertphillips391395d2016-03-02 09:26:36 -0800530 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700531 static constexpr GrUserStencilSettings kDrawOutsideElement(
532 GrUserStencilSettings::StaticInit<
533 0x0000,
534 GrUserStencilTest::kEqual,
535 0xffff,
536 GrUserStencilOp::kZero,
537 GrUserStencilOp::kZero,
538 0xffff>()
539 );
cdalton862cff32016-05-12 15:09:48 -0700540 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800541 op, !invert, false,
542 translate,
csmartdaltond211e782016-08-15 11:17:19 -0700543 SkRect::Make(reducedClip.ibounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800544 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000545 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000546 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800547 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800548 GrPaint paint;
549 paint.setAntiAlias(element->isAA());
550 paint.setCoverageSetOpXPFactory(op, false);
551
cdalton846c0512016-05-13 10:25:00 -0700552 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553 }
554 }
555
robertphillipsc99b8f02016-05-15 07:53:35 -0700556 sk_sp<GrTexture> texture(dc->asTexture());
557 SkASSERT(texture);
558 texture->resourcePriv().setUniqueKey(key);
559 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560}
561
562////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000563// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000564// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700565bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
566 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700567 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700568 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700569 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570
robertphillips976f5f02016-06-03 10:59:20 -0700571 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
572 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700573 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000574 return false;
575 }
576
robertphillips976f5f02016-06-03 10:59:20 -0700577 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdalton8d3f92a2016-08-17 09:39:38 -0700578 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700579 clipSpaceToStencilOffset)) {
csmartdalton8d3f92a2016-08-17 09:39:38 -0700580 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700581 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000582 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
583 SkVector translate = {
584 SkIntToScalar(clipSpaceToStencilOffset.fX),
585 SkIntToScalar(clipSpaceToStencilOffset.fY)
586 };
joshualitt8059eb92014-12-29 15:10:07 -0800587 SkMatrix viewMatrix;
588 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
bsalomon@google.com9f131742012-12-13 20:43:56 +0000590 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdaltond211e782016-08-15 11:17:19 -0700591 SkIRect stencilSpaceIBounds(reducedClip.ibounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000592 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700593 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
csmartdaltond211e782016-08-15 11:17:19 -0700595 bool insideClip = InitialState::kAllIn == reducedClip.initialState();
csmartdalton77f2fae2016-08-08 09:55:06 -0700596 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597
598 // walk through each clip element and perform its set op
599 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700600 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000601 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700602 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800603
tomhudson@google.com8afae612012-08-14 15:03:35 +0000604 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000605 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700606 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000608 // This will be used to determine whether the clip shape can be rendered into the
609 // stencil with arbitrary stencil settings.
610 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000612 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613
halcanary96fcdcc2015-08-27 07:41:13 -0700614 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000615 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000616 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000617 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000619 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000620 element->asPath(&clipPath);
621 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000622 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000623 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000624 }
robertphillips68737822015-10-29 12:12:21 -0700625
bsalomon8acedde2016-06-24 10:42:16 -0700626 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700627 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700628 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700629 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700630 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700631 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700632 canDrawArgs.fHasUserStencilSettings = false;
633 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700634
csmartdaltonc6f411e2016-08-05 22:32:12 -0700635 GrDrawingManager* dm = context->drawingManager();
636 pr = dm->getPathRenderer(canDrawArgs, false,
637 GrPathRendererChain::kStencilOnly_DrawType,
638 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700639 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000640 return false;
641 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642 }
643
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000644 bool canRenderDirectToStencil =
645 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700646 bool drawDirectToClip; // Given the renderer, the element,
647 // fill rule, and set operation should
648 // we render the element directly to
649 // stencil bit used for clipping.
650 GrUserStencilSettings const* const* stencilPasses =
651 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
652 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653
654 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700655 if (!drawDirectToClip) {
656 static constexpr GrUserStencilSettings kDrawToStencil(
657 GrUserStencilSettings::StaticInit<
658 0x0000,
659 GrUserStencilTest::kAlways,
660 0xffff,
661 GrUserStencilOp::kIncMaybeClamp,
662 GrUserStencilOp::kIncMaybeClamp,
663 0xffff>()
664 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000665 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700666 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
667 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000669 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700670 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000671 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700672 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700673 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700674 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700675
676 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700677 args.fResourceProvider = context->resourceProvider();
678 args.fPaint = &paint;
679 args.fUserStencilSettings = &kDrawToStencil;
680 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700681 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700682 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700683 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700684 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700685 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700686 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000687 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700688 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700689 args.fResourceProvider = context->resourceProvider();
690 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700691 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700692 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700693 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700694 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700695 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000696 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 }
698 }
699 }
700
csmartdaltond211e782016-08-15 11:17:19 -0700701 // Just enable stencil clip. The passes choose whether or not they will actually use it.
702 clip.enableStencilClip();
703
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 // now we modify the clip bit by rendering either the clip
705 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700706 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
cdalton93a379b2016-05-11 13:58:08 -0700707 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000708 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700709 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
710 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700712 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700713 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700714 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700715 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700716 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700717 args.fResourceProvider = context->resourceProvider();
718 args.fPaint = &paint;
719 args.fUserStencilSettings = *pass;
720 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700721 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700722 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700723 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700724 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700725 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700726 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 }
728 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000729 // The view matrix is setup to do clip space -> stencil space translation, so
730 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700731 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdaltond211e782016-08-15 11:17:19 -0700732 SkRect::Make(reducedClip.ibounds()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 }
734 }
735 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 return true;
738}
739
bsalomon@google.com411dad02012-06-05 20:24:20 +0000740////////////////////////////////////////////////////////////////////////////////
csmartdaltonc6f411e2016-08-05 22:32:12 -0700741sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdalton77f2fae2016-08-08 09:55:06 -0700742 const GrReducedClip& reducedClip,
743 const SkVector& clipToMaskOffset) {
bsalomon473addf2015-10-02 07:49:05 -0700744 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700745 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700746 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700747 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000748 }
749
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000750 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
751 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700752 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000753
robertphillips0152d732016-05-20 06:38:43 -0700754 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000755
joshualitt8059eb92014-12-29 15:10:07 -0800756 // Set the matrix so that rendered clip elements are transformed to mask space from clip
757 // space.
758 SkMatrix translate;
759 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800760
robertphillips98377402016-05-13 05:47:23 -0700761 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700762 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000763
csmartdalton77f2fae2016-08-08 09:55:06 -0700764 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000765 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000766 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000767
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000768 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
769 // Intersect and reverse difference require modifying pixels outside of the geometry
770 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
771 // but leave the pixels inside the geometry alone. For reverse difference we invert all
772 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000773 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700774 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000775 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700776 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000777 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000778 SkPath clipPath;
779 element->asPath(&clipPath);
780 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700781 GrShape shape(clipPath, GrStyle::SimpleFill());
782 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000783 continue;
784 }
785
786 // The other ops (union, xor, diff) only affect pixels inside
787 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000788 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700789 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000790 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000791 SkPath path;
792 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700793 GrShape shape(path, GrStyle::SimpleFill());
794 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000795 }
796 }
797
krajcevskiad1dc582014-06-10 15:06:47 -0700798 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800799 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700800 desc.fWidth = reducedClip.width();
801 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800802 desc.fConfig = kAlpha_8_GrPixelConfig;
803
robertphillips0152d732016-05-20 06:38:43 -0700804 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800805 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700806 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700807 }
robertphillips391395d2016-03-02 09:26:36 -0800808 result->resourcePriv().setUniqueKey(key);
809
robertphillipsc99b8f02016-05-15 07:53:35 -0700810 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000811
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000812 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000813}