blob: d85db1109aa6e1b7ec6f2416ec5a6a6244e9d296 [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"
robertphillips391395d2016-03-02 09:26:36 -080013#include "GrDrawContextPriv.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
bsalomoncb31e512016-08-26 10:48:19 -070045bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, bool* aa) const {
46 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 }
57 if (fStack->isRRect(*rtBounds, rr, aa)) {
58 if (origin) {
59 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
60 }
61 return true;
62 }
63 return false;
64}
65
csmartdaltonc6f411e2016-08-05 22:32:12 -070066void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
67 bool* isIntersectionOfRects) const {
68 if (!fStack) {
69 devResult->setXYWH(0, 0, width, height);
70 if (isIntersectionOfRects) {
71 *isIntersectionOfRects = true;
72 }
73 return;
74 }
75 SkRect devBounds;
76 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
77 isIntersectionOfRects);
78 devBounds.roundOut(devResult);
79}
80
bsalomon@google.com51a62862012-11-26 21:19:43 +000081////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000083// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070084static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
85 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000086 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080087 // We use device coords to compute the texture coordinates. We set our matrix to be a
88 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000089 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000091 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000092
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000093 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070094 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070095 result,
brianosman54f30c12016-07-18 10:53:52 -070096 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070097 mat,
98 GrTextureDomain::MakeTexelDomain(result, domainTexels),
99 GrTextureDomain::kDecal_Mode,
100 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -0700101 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000102}
103
robertphillips3f7357f2015-10-27 07:17:33 -0700104// Does the path in 'element' require SW rendering? If so, return true (and,
105// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
106// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -0700107bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
108 bool hasUserStencilSettings,
109 const GrDrawContext* drawContext,
110 const SkMatrix& viewMatrix,
111 const Element* element,
112 GrPathRenderer** prOut,
113 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700114 if (Element::kRect_Type == element->getType()) {
115 // rects can always be drawn directly w/o using the software path
116 // TODO: skip rrects once we're drawing them directly.
117 if (prOut) {
118 *prOut = nullptr;
119 }
120 return false;
121 } else {
122 // We shouldn't get here with an empty clip element.
123 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700124
robertphillips3f7357f2015-10-27 07:17:33 -0700125 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
126 SkPath path;
127 element->asPath(&path);
128 if (path.isInverseFillType()) {
129 path.toggleInverseFillType();
130 }
halcanary9d524f22016-03-29 09:03:52 -0700131
robertphillips3f7357f2015-10-27 07:17:33 -0700132 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700133
robertphillips423e3372015-10-27 09:23:38 -0700134 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700135 type = element->isAA()
136 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
137 : GrPathRendererChain::kStencilAndColor_DrawType;
138 } else {
139 type = element->isAA()
140 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700141 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700142 }
halcanary9d524f22016-03-29 09:03:52 -0700143
bsalomon8acedde2016-06-24 10:42:16 -0700144 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700145 GrPathRenderer::CanDrawPathArgs canDrawArgs;
146 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
147 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700148 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700149 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700150 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700151 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700152
robertphillips3f7357f2015-10-27 07:17:33 -0700153 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700154 GrPathRenderer* pr =
155 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700156 if (prOut) {
157 *prOut = pr;
158 }
159 return SkToBool(!pr);
160 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000161}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000162
robertphillips@google.comfa662942012-05-17 12:20:22 +0000163/*
164 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
165 * will be used on any element. If so, it returns true to indicate that the
166 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
167 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700168bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
169 bool hasUserStencilSettings,
170 const GrDrawContext* drawContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700171 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000172 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000173 // a clip gets complex enough it can just be done in SW regardless
174 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000175
joshualitt8059eb92014-12-29 15:10:07 -0800176 // Set the matrix so that rendered clip elements are transformed to mask space from clip
177 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700178 SkMatrix translate;
179 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800180
csmartdaltonbde96c62016-08-31 12:54:46 -0700181 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000182 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700183
reed73603f32016-09-20 08:42:38 -0700184 SkCanvas::ClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700185 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700186 bool needsStencil = invert ||
reed73603f32016-09-20 08:42:38 -0700187 SkCanvas::kIntersect_Op == op || SkCanvas::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700188
robertphillips59cf61a2016-07-13 09:18:21 -0700189 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700190 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700191 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000192 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000193 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000194 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000195}
196
csmartdalton77f2fae2016-08-08 09:55:06 -0700197static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700198 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700199 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700200 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700201 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700203 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700204 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700205 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700206 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700207 while (iter.get()) {
reed73603f32016-09-20 08:42:38 -0700208 SkCanvas::ClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 bool invert;
210 bool skip = false;
211 switch (op) {
212 case SkRegion::kReplace_Op:
213 SkASSERT(iter.get() == elements.head());
214 // Fallthrough, handled same as intersect.
215 case SkRegion::kIntersect_Op:
216 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700217 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 skip = true;
219 }
220 break;
221 case SkRegion::kDifference_Op:
222 invert = true;
223 // We don't currently have a cheap test for whether a rect is fully outside an
224 // element's primitive, so don't attempt to set skip.
225 break;
226 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700227 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000229 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700230 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800231 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700232 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700233 return false;
bsalomona912dde2015-10-14 15:01:50 -0700234 }
joshualittb0a8a372014-09-23 09:50:21 -0700235 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700236 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700238 edgeType =
239 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
bsalomona912dde2015-10-14 15:01:50 -0700241
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000242 switch (iter.get()->getType()) {
243 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700244 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
245 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 break;
247 case SkClipStack::Element::kRRect_Type: {
248 SkRRect rrect = iter.get()->getRRect();
249 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700250 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000251 break;
252 }
253 case SkClipStack::Element::kRect_Type: {
254 SkRect rect = iter.get()->getRect();
255 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700256 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000257 break;
258 }
259 default:
260 break;
261 }
bungeman06ca8ec2016-06-09 08:01:03 -0700262 if (!fps.back()) {
263 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000264 }
265 }
mtklein217daa72014-07-02 12:55:21 -0700266 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000267 }
268
bsalomon0b5b6b22015-10-14 08:31:34 -0700269 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700270 if (fps.count()) {
271 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000272 }
bungeman06ca8ec2016-06-09 08:01:03 -0700273 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000274}
275
robertphillips@google.comf294b772012-04-27 14:29:26 +0000276////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000277// sort out what kind of clip mask needs to be created: alpha, stencil,
278// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700279bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
280 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700281 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700282 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700283 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000284
csmartdaltoncbecb082016-07-22 08:59:08 -0700285 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700286 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700287 return false;
288 }
289
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700290 GrRenderTarget* rt = drawContext->accessRenderTarget();
291
csmartdaltonc6f411e2016-08-05 22:32:12 -0700292 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
293 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700294
csmartdalton77f2fae2016-08-08 09:55:06 -0700295 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700296 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
297 rt->renderTargetPriv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000298
csmartdaltond211e782016-08-15 11:17:19 -0700299 if (reducedClip.hasIBounds() &&
300 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
301 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
302 scissorSpaceIBounds.offset(-fOrigin);
303 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700304 }
cdalton93a379b2016-05-11 13:58:08 -0700305
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700306 if (!reducedClip.windowRectangles().empty()) {
307 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
308 GrWindowRectsState::Mode::kExclusive);
309 }
310
csmartdaltond211e782016-08-15 11:17:19 -0700311 if (reducedClip.elements().isEmpty()) {
312 return InitialState::kAllIn == reducedClip.initialState();
313 }
314
315 SkASSERT(reducedClip.hasIBounds());
316
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.
robertphillips976f5f02016-06-03 10:59:20 -0700328 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
329 if (disallowAnalyticAA && !drawContext->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.
csmartdalton77f2fae2016-08-08 09:55:06 -0700345 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700346 sk_sp<GrTexture> result;
csmartdaltonbde96c62016-08-31 12:54:46 -0700347 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext, 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);
robertphillips391395d2016-03-02 09:26:36 -0800353 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700354 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000355 }
356
bsalomon49f085d2014-09-05 13:34:00 -0700357 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000358 // The mask's top left coord should be pinned to the rounded-out top left corner of
359 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700360 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700361 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700362 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363 return true;
364 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000365 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonbde96c62016-08-31 12:54:46 -0700369 // TODO: these need to be swapped over to using a StencilAttachmentProxy
370 GrStencilAttachment* stencilAttachment =
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700371 context->resourceProvider()->attachStencilAttachment(rt);
csmartdaltonbde96c62016-08-31 12:54:46 -0700372 if (nullptr == stencilAttachment) {
373 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.
csmartdaltonbde96c62016-08-31 12:54:46 -0700380 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
381 fOrigin)) {
382 reducedClip.drawStencilClipMask(context, drawContext, fOrigin);
383 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
384 fOrigin);
385 }
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;
397 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
398 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
399}
400
csmartdaltonc6f411e2016-08-05 22:32:12 -0700401sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700402 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800403 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700404 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700405 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700406 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700407 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000408 }
409
robertphillips48fde9c2016-09-06 05:20:20 -0700410 sk_sp<GrDrawContext> dc(context->makeDrawContextWithFallback(SkBackingFit::kApprox,
411 reducedClip.width(),
412 reducedClip.height(),
413 kAlpha_8_GrPixelConfig,
414 nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800415 if (!dc) {
416 return nullptr;
417 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000418
csmartdaltonbde96c62016-08-31 12:54:46 -0700419 if (!reducedClip.drawAlphaClipMask(dc.get())) {
420 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421 }
422
robertphillipsc99b8f02016-05-15 07:53:35 -0700423 sk_sp<GrTexture> texture(dc->asTexture());
424 SkASSERT(texture);
425 texture->resourcePriv().setUniqueKey(key);
426 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427}
428
csmartdaltonc6f411e2016-08-05 22:32:12 -0700429sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700430 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700431 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700432 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700433 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700434 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000435 }
436
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000437 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
438 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700439 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440
robertphillips0152d732016-05-20 06:38:43 -0700441 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000442
joshualitt8059eb92014-12-29 15:10:07 -0800443 // Set the matrix so that rendered clip elements are transformed to mask space from clip
444 // space.
445 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700446 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800447
robertphillips98377402016-05-13 05:47:23 -0700448 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700449 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000450
csmartdalton77f2fae2016-08-08 09:55:06 -0700451 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700453 SkCanvas::ClipOp op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000454
reed73603f32016-09-20 08:42:38 -0700455 if (SkCanvas::kIntersect_Op == op || SkCanvas::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000456 // Intersect and reverse difference require modifying pixels outside of the geometry
457 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
458 // but leave the pixels inside the geometry alone. For reverse difference we invert all
459 // the pixels before clearing the ones outside the geometry.
reed73603f32016-09-20 08:42:38 -0700460 if (SkCanvas::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700461 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000462 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700463 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000464 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000465 SkPath clipPath;
466 element->asPath(&clipPath);
467 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700468 GrShape shape(clipPath, GrStyle::SimpleFill());
469 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000470 continue;
471 }
472
473 // The other ops (union, xor, diff) only affect pixels inside
474 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000475 if (Element::kRect_Type == element->getType()) {
reed73603f32016-09-20 08:42:38 -0700476 helper.drawRect(element->getRect(), (SkRegion::Op)op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000477 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000478 SkPath path;
479 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700480 GrShape shape(path, GrStyle::SimpleFill());
reed73603f32016-09-20 08:42:38 -0700481 helper.drawShape(shape, (SkRegion::Op)op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000482 }
483 }
484
krajcevskiad1dc582014-06-10 15:06:47 -0700485 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800486 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700487 desc.fWidth = reducedClip.width();
488 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800489 desc.fConfig = kAlpha_8_GrPixelConfig;
490
robertphillips0152d732016-05-20 06:38:43 -0700491 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800492 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700493 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700494 }
robertphillips391395d2016-03-02 09:26:36 -0800495 result->resourcePriv().setUniqueKey(key);
496
robertphillipsc99b8f02016-05-15 07:53:35 -0700497 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000498
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000500}