blob: 1172bc6a7b42308dead49cfc2b84670387dd325b [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"
csmartdaltonbde96c62016-08-31 12:54:46 -070011#include "GrContextPriv.h"
robertphillips68737822015-10-29 12:12:21 -070012#include "GrDrawingManager.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070014#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070015#include "GrGpuResourcePriv.h"
csmartdalton28341fa2016-08-17 10:00:21 -070016#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080019#include "effects/GrConvexPolyEffect.h"
20#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000022
bsalomon@google.com8182fa02012-12-04 14:06:06 +000023typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070024typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070025typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000026
robertphillips976f5f02016-06-03 10:59:20 -070027static const int kMaxAnalyticElements = 4;
28
csmartdaltonc6f411e2016-08-05 22:32:12 -070029bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070030 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070031 return true;
32 }
33 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
34 SkIntToScalar(fOrigin.y())));
35}
36
bsalomon7f0d9f32016-08-15 14:49:10 -070037bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070038 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070039 return true;
40 }
41 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
42 SkIntToScalar(fOrigin.fY)));
43}
44
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050045bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070046 if (!fStack) {
47 return false;
48 }
49 const SkRect* rtBounds = &origRTBounds;
50 SkRect tempRTBounds;
51 bool origin = fOrigin.fX || fOrigin.fY;
52 if (origin) {
53 tempRTBounds = origRTBounds;
54 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
55 rtBounds = &tempRTBounds;
56 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050057 bool isAA;
58 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
59 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070060 if (origin) {
61 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
62 }
63 return true;
64 }
65 return false;
66}
67
csmartdaltonc6f411e2016-08-05 22:32:12 -070068void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
69 bool* isIntersectionOfRects) const {
70 if (!fStack) {
71 devResult->setXYWH(0, 0, width, height);
72 if (isIntersectionOfRects) {
73 *isIntersectionOfRects = true;
74 }
75 return;
76 }
77 SkRect devBounds;
78 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
79 isIntersectionOfRects);
80 devBounds.roundOut(devResult);
81}
82
bsalomon@google.com51a62862012-11-26 21:19:43 +000083////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040084// set up the draw state to enable the aa clipping mask.
bungeman06ca8ec2016-06-09 08:01:03 -070085static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
86 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000087 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -040088 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(result, domainTexels,
89 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000090}
91
robertphillips3f7357f2015-10-27 07:17:33 -070092// Does the path in 'element' require SW rendering? If so, return true (and,
93// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
94// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070095bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
96 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040097 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070098 const SkMatrix& viewMatrix,
99 const Element* element,
100 GrPathRenderer** prOut,
101 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700102 if (Element::kRect_Type == element->getType()) {
103 // rects can always be drawn directly w/o using the software path
104 // TODO: skip rrects once we're drawing them directly.
105 if (prOut) {
106 *prOut = nullptr;
107 }
108 return false;
109 } else {
110 // We shouldn't get here with an empty clip element.
111 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700112
robertphillips3f7357f2015-10-27 07:17:33 -0700113 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
114 SkPath path;
115 element->asPath(&path);
116 if (path.isInverseFillType()) {
117 path.toggleInverseFillType();
118 }
halcanary9d524f22016-03-29 09:03:52 -0700119
robertphillips3f7357f2015-10-27 07:17:33 -0700120 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700121
robertphillips423e3372015-10-27 09:23:38 -0700122 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700123 type = element->isAA()
124 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
125 : GrPathRendererChain::kStencilAndColor_DrawType;
126 } else {
127 type = element->isAA()
128 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700129 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700130 }
halcanary9d524f22016-03-29 09:03:52 -0700131
bsalomon8acedde2016-06-24 10:42:16 -0700132 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700133 GrPathRenderer::CanDrawPathArgs canDrawArgs;
134 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
135 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700136 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500137 if (!element->isAA()) {
138 canDrawArgs.fAAType = GrAAType::kNone;
139 } else if (renderTargetContext->isUnifiedMultisampled()) {
140 canDrawArgs.fAAType = GrAAType::kMSAA;
141 } else if (renderTargetContext->isStencilBufferMultisampled()){
142 canDrawArgs.fAAType = GrAAType::kMixedSamples;
143 } else {
144 canDrawArgs.fAAType = GrAAType::kCoverage;
145 }
cdalton93a379b2016-05-11 13:58:08 -0700146 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700147
robertphillips3f7357f2015-10-27 07:17:33 -0700148 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700149 GrPathRenderer* pr =
150 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700151 if (prOut) {
152 *prOut = pr;
153 }
154 return SkToBool(!pr);
155 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000156}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000157
robertphillips@google.comfa662942012-05-17 12:20:22 +0000158/*
159 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
160 * will be used on any element. If so, it returns true to indicate that the
161 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
162 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700163bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
164 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400165 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700166 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000167 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000168 // a clip gets complex enough it can just be done in SW regardless
169 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000170
joshualitt8059eb92014-12-29 15:10:07 -0800171 // Set the matrix so that rendered clip elements are transformed to mask space from clip
172 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700173 SkMatrix translate;
174 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800175
csmartdaltonbde96c62016-08-31 12:54:46 -0700176 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000177 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700178
Mike Reedc1f77742016-12-09 09:00:50 -0500179 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700180 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700181 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500182 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700183
robertphillips59cf61a2016-07-13 09:18:21 -0700184 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400185 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700186 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000187 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000188 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000189 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000190}
191
csmartdalton77f2fae2016-08-08 09:55:06 -0700192static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700193 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700194 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700195 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700196 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000197 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700198 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700199 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700200 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700201 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700202 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500203 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 bool invert;
205 bool skip = false;
206 switch (op) {
207 case SkRegion::kReplace_Op:
208 SkASSERT(iter.get() == elements.head());
209 // Fallthrough, handled same as intersect.
210 case SkRegion::kIntersect_Op:
211 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700212 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 skip = true;
214 }
215 break;
216 case SkRegion::kDifference_Op:
217 invert = true;
218 // We don't currently have a cheap test for whether a rect is fully outside an
219 // element's primitive, so don't attempt to set skip.
220 break;
221 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700222 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000224 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700225 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800226 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700227 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700228 return false;
bsalomona912dde2015-10-14 15:01:50 -0700229 }
joshualittb0a8a372014-09-23 09:50:21 -0700230 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700231 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000232 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700233 edgeType =
234 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235 }
bsalomona912dde2015-10-14 15:01:50 -0700236
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 switch (iter.get()->getType()) {
238 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700239 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
240 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 break;
242 case SkClipStack::Element::kRRect_Type: {
243 SkRRect rrect = iter.get()->getRRect();
244 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700245 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 break;
247 }
248 case SkClipStack::Element::kRect_Type: {
249 SkRect rect = iter.get()->getRect();
250 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700251 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000252 break;
253 }
254 default:
255 break;
256 }
bungeman06ca8ec2016-06-09 08:01:03 -0700257 if (!fps.back()) {
258 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000259 }
260 }
mtklein217daa72014-07-02 12:55:21 -0700261 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000262 }
263
bsalomon0b5b6b22015-10-14 08:31:34 -0700264 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700265 if (fps.count()) {
266 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000267 }
bungeman06ca8ec2016-06-09 08:01:03 -0700268 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000269}
270
robertphillips@google.comf294b772012-04-27 14:29:26 +0000271////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000272// sort out what kind of clip mask needs to be created: alpha, stencil,
273// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400274bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
275 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700276 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700277 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700278 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000279
Robert Phillips784b7bf2016-12-09 13:35:02 -0500280 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(),
281 renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700282 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700283 return false;
284 }
285
csmartdaltonc6f411e2016-08-05 22:32:12 -0700286 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
287 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700288
csmartdalton77f2fae2016-08-08 09:55:06 -0700289 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700290 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500291 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000292
csmartdaltond211e782016-08-15 11:17:19 -0700293 if (reducedClip.hasIBounds() &&
294 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
295 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
296 scissorSpaceIBounds.offset(-fOrigin);
297 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700298 }
cdalton93a379b2016-05-11 13:58:08 -0700299
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700300 if (!reducedClip.windowRectangles().empty()) {
301 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
302 GrWindowRectsState::Mode::kExclusive);
303 }
304
csmartdaltond211e782016-08-15 11:17:19 -0700305 if (reducedClip.elements().isEmpty()) {
306 return InitialState::kAllIn == reducedClip.initialState();
307 }
308
csmartdalton3affdc12016-10-28 12:01:10 -0700309#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700310 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500311 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
312 renderTargetContext->height());
csmartdalton3affdc12016-10-28 12:01:10 -0700313 SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y());
314 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
315#endif
csmartdaltond211e782016-08-15 11:17:19 -0700316
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000317 // An element count of 4 was chosen because of the common pattern in Blink of:
318 // isect RR
319 // diff RR
320 // isect convex_poly
321 // isect convex_poly
322 // when drawing rounded div borders. This could probably be tuned based on a
323 // configuration's relative costs of switching RTs to generate a mask vs
324 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700325 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800326 // When there are multiple samples we want to do per-sample clipping, not compute a
327 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400328 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
329 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700330 // With a single color sample, any coverage info is lost from color once it hits the
331 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
332 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700333 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700334 }
bungeman06ca8ec2016-06-09 08:01:03 -0700335 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700336 if (reducedClip.requiresAA() &&
337 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
338 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700339 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000340 return true;
341 }
342 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000343
cdaltonede75742015-11-11 15:27:57 -0800344 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400345 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700346 sk_sp<GrTexture> result;
Brian Osman11052242016-10-27 14:47:55 -0400347 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000348 // The clip geometry is complex enough that it will be more efficient to create it
349 // entirely in software
csmartdaltonbde96c62016-08-31 12:54:46 -0700350 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000351 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700352 result = CreateAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 }
354
bsalomon49f085d2014-09-05 13:34:00 -0700355 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000356 // The mask's top left coord should be pinned to the rounded-out top left corner of
357 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700358 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700359 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700360 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361 return true;
362 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
Robert Phillipsec2249f2016-11-09 08:54:35 -0500366 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500367 if (!rt) {
368 return true;
369 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500370
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000371 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700372 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700373 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
374 return true;
375 }
376
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700377 // This relies on the property that a reduced sub-rect of the last clip will contain all the
378 // relevant window rectangles that were in the last clip. This subtle requirement will go away
379 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700380 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
381 reducedClip.ibounds(), fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400382 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdalton7cdda992016-11-01 07:03:03 -0700383 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
384 fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700385 }
csmartdaltond211e782016-08-15 11:17:19 -0700386 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000387 return true;
388}
389
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000390////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700391// Create a 8-bit clip mask in alpha
392
393static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
394 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
395 GrUniqueKey::Builder builder(key, kDomain, 3);
396 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700397 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
398 // sometimes result in negative coordinates from clip space.
399 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
400 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700401}
402
csmartdaltonc6f411e2016-08-05 22:32:12 -0700403sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700404 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800405 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700406 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700407 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700408 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700409 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000410 }
411
Brian Osman11052242016-10-27 14:47:55 -0400412 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
413 SkBackingFit::kApprox,
414 reducedClip.width(),
415 reducedClip.height(),
416 kAlpha_8_GrPixelConfig,
417 nullptr));
418 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800419 return nullptr;
420 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421
Brian Osman11052242016-10-27 14:47:55 -0400422 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700423 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000424 }
425
Brian Osman11052242016-10-27 14:47:55 -0400426 sk_sp<GrTexture> texture(rtc->asTexture());
Robert Phillipse60ad622016-11-17 10:22:48 -0500427 if (!texture) {
428 return nullptr;
429 }
430
robertphillipsc99b8f02016-05-15 07:53:35 -0700431 texture->resourcePriv().setUniqueKey(key);
432 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433}
434
csmartdaltonc6f411e2016-08-05 22:32:12 -0700435sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700436 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700437 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700438 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700439 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700440 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000441 }
442
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
444 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700445 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446
robertphillips0152d732016-05-20 06:38:43 -0700447 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000448
joshualitt8059eb92014-12-29 15:10:07 -0800449 // Set the matrix so that rendered clip elements are transformed to mask space from clip
450 // space.
451 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700452 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800453
csmartdalton3affdc12016-10-28 12:01:10 -0700454 if (!helper.init(maskSpaceIBounds, &translate)) {
455 return nullptr;
456 }
csmartdaltond211e782016-08-15 11:17:19 -0700457 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000458
csmartdalton77f2fae2016-08-08 09:55:06 -0700459 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500461 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500462 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000463
Mike Reedc1f77742016-12-09 09:00:50 -0500464 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 // Intersect and reverse difference require modifying pixels outside of the geometry
466 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
467 // but leave the pixels inside the geometry alone. For reverse difference we invert all
468 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500469 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700470 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000471 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500472 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000473 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000474 SkPath clipPath;
475 element->asPath(&clipPath);
476 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700477 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500478 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000479 continue;
480 }
481
482 // The other ops (union, xor, diff) only affect pixels inside
483 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000484 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500485 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000486 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000487 SkPath path;
488 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700489 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500490 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000491 }
492 }
493
krajcevskiad1dc582014-06-10 15:06:47 -0700494 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800495 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700496 desc.fWidth = reducedClip.width();
497 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800498 desc.fConfig = kAlpha_8_GrPixelConfig;
499
robertphillips0152d732016-05-20 06:38:43 -0700500 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800501 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700502 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700503 }
robertphillips391395d2016-03-02 09:26:36 -0800504 result->resourcePriv().setUniqueKey(key);
505
robertphillipsc99b8f02016-05-15 07:53:35 -0700506 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000507
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000509}