blob: 3a31d0baf783e894a90b908b1b93ad8927b1a792 [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"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050020#include "GrTextureProxy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080021#include "effects/GrConvexPolyEffect.h"
22#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "effects/GrTextureDomain.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050024#include "SkClipOpPriv.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000025
bsalomon@google.com8182fa02012-12-04 14:06:06 +000026typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070027typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070028typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
robertphillips976f5f02016-06-03 10:59:20 -070030static const int kMaxAnalyticElements = 4;
Brian Salomon19f0ed52017-01-06 13:54:58 -050031const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070032
csmartdaltonc6f411e2016-08-05 22:32:12 -070033bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070034 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070035 return true;
36 }
Brian Salomon9a767722017-03-13 17:57:28 -040037 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070038}
39
bsalomon7f0d9f32016-08-15 14:49:10 -070040bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070041 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070042 return true;
43 }
Brian Salomon9a767722017-03-13 17:57:28 -040044 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070045}
46
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050047bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070048 if (!fStack) {
49 return false;
50 }
51 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050052 bool isAA;
53 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
54 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070055 return true;
56 }
57 return false;
58}
59
csmartdaltonc6f411e2016-08-05 22:32:12 -070060void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
61 bool* isIntersectionOfRects) const {
62 if (!fStack) {
63 devResult->setXYWH(0, 0, width, height);
64 if (isIntersectionOfRects) {
65 *isIntersectionOfRects = true;
66 }
67 return;
68 }
69 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040070 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070071 devBounds.roundOut(devResult);
72}
73
bsalomon@google.com51a62862012-11-26 21:19:43 +000074////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040075// set up the draw state to enable the aa clipping mask.
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040076static sk_sp<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
bungeman06ca8ec2016-06-09 08:01:03 -070077 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000078 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040079 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
Brian Salomon2ebd0c82016-10-03 17:15:28 -040080 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000081}
82
robertphillips3f7357f2015-10-27 07:17:33 -070083// Does the path in 'element' require SW rendering? If so, return true (and,
84// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
85// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070086bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
87 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040088 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070089 const SkMatrix& viewMatrix,
90 const Element* element,
91 GrPathRenderer** prOut,
92 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070093 if (Element::kRect_Type == element->getType()) {
94 // rects can always be drawn directly w/o using the software path
95 // TODO: skip rrects once we're drawing them directly.
96 if (prOut) {
97 *prOut = nullptr;
98 }
99 return false;
100 } else {
101 // We shouldn't get here with an empty clip element.
102 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700103
robertphillips3f7357f2015-10-27 07:17:33 -0700104 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
105 SkPath path;
106 element->asPath(&path);
107 if (path.isInverseFillType()) {
108 path.toggleInverseFillType();
109 }
halcanary9d524f22016-03-29 09:03:52 -0700110
Brian Salomon82125e92016-12-10 09:35:48 -0500111 GrPathRendererChain::DrawType type =
112 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
113 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700114
bsalomon8acedde2016-06-24 10:42:16 -0700115 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700116 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700117 canDrawArgs.fCaps = context->caps();
robertphillips68737822015-10-29 12:12:21 -0700118 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700119 canDrawArgs.fShape = &shape;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400120 canDrawArgs.fAAType = GrChooseAAType(GrBoolToAA(element->isAA()),
121 renderTargetContext->fsaaType(),
Brian Salomone225b562017-06-14 13:00:03 -0400122 GrAllowMixedSamples::kYes,
123 *context->caps());
cdalton93a379b2016-05-11 13:58:08 -0700124 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700125
robertphillips3f7357f2015-10-27 07:17:33 -0700126 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700127 GrPathRenderer* pr =
128 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700129 if (prOut) {
130 *prOut = pr;
131 }
132 return SkToBool(!pr);
133 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000134}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000135
robertphillips@google.comfa662942012-05-17 12:20:22 +0000136/*
137 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
138 * will be used on any element. If so, it returns true to indicate that the
139 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
140 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700141bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
142 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400143 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700144 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000145 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000146 // a clip gets complex enough it can just be done in SW regardless
147 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000148
Eric Karl5c779752017-05-08 12:02:07 -0700149 // If we're avoiding stencils, always use SW:
150 if (context->caps()->avoidStencilBuffers())
151 return true;
152
joshualitt8059eb92014-12-29 15:10:07 -0800153 // Set the matrix so that rendered clip elements are transformed to mask space from clip
154 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700155 SkMatrix translate;
156 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800157
csmartdaltonbde96c62016-08-31 12:54:46 -0700158 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700160
Mike Reedc1f77742016-12-09 09:00:50 -0500161 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700162 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700163 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500164 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700165
robertphillips59cf61a2016-07-13 09:18:21 -0700166 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400167 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700168 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000169 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000170 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000171 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000172}
173
csmartdalton77f2fae2016-08-08 09:55:06 -0700174static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700175 bool abortIfAA,
Brian Salomon9a767722017-03-13 17:57:28 -0400176 const SkRect& drawDevBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700177 sk_sp<GrFragmentProcessor>* resultFP) {
bsalomon0ba8c242015-10-07 09:20:28 -0700178 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700179 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700180 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700181 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500182 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000183 bool invert;
184 bool skip = false;
185 switch (op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500186 case kReplace_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000187 SkASSERT(iter.get() == elements.head());
188 // Fallthrough, handled same as intersect.
Mike Reedebfce6d2016-12-12 10:02:12 -0500189 case kIntersect_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 invert = false;
Brian Salomon9a767722017-03-13 17:57:28 -0400191 if (iter.get()->contains(drawDevBounds)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 skip = true;
193 }
194 break;
Mike Reedebfce6d2016-12-12 10:02:12 -0500195 case kDifference_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 invert = true;
197 // We don't currently have a cheap test for whether a rect is fully outside an
198 // element's primitive, so don't attempt to set skip.
199 break;
200 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700201 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700204 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800205 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700206 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700207 return false;
bsalomona912dde2015-10-14 15:01:50 -0700208 }
joshualittb0a8a372014-09-23 09:50:21 -0700209 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700210 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700212 edgeType =
213 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
bsalomona912dde2015-10-14 15:01:50 -0700215
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 switch (iter.get()->getType()) {
217 case SkClipStack::Element::kPath_Type:
Brian Salomon9a767722017-03-13 17:57:28 -0400218 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 break;
220 case SkClipStack::Element::kRRect_Type: {
Brian Salomon9a767722017-03-13 17:57:28 -0400221 fps.emplace_back(GrRRectEffect::Make(edgeType, iter.get()->getRRect()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 break;
223 }
224 case SkClipStack::Element::kRect_Type: {
Brian Salomon9a767722017-03-13 17:57:28 -0400225 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getRect()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000226 break;
227 }
228 default:
229 break;
230 }
bungeman06ca8ec2016-06-09 08:01:03 -0700231 if (!fps.back()) {
232 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 }
234 }
mtklein217daa72014-07-02 12:55:21 -0700235 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000236 }
237
bsalomon0b5b6b22015-10-14 08:31:34 -0700238 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700239 if (fps.count()) {
240 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 }
bungeman06ca8ec2016-06-09 08:01:03 -0700242 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243}
244
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000246// sort out what kind of clip mask needs to be created: alpha, stencil,
247// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400248bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400249 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
250 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400251 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
252 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700253 return false;
254 }
255
Brian Salomon510dd422017-03-16 12:15:22 -0400256 if (!fStack || fStack->isWideOpen()) {
257 return true;
258 }
259
Brian Salomon9a767722017-03-13 17:57:28 -0400260 const GrReducedClip reducedClip(*fStack, devBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500261 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000262
Brian Salomon9a767722017-03-13 17:57:28 -0400263 if (reducedClip.hasIBounds() && !GrClip::IsInsideClip(reducedClip.ibounds(), devBounds)) {
Brian Salomon97180af2017-03-14 13:42:58 -0400264 out->addScissor(reducedClip.ibounds(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700265 }
cdalton93a379b2016-05-11 13:58:08 -0700266
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700267 if (!reducedClip.windowRectangles().empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400268 out->addWindowRectangles(reducedClip.windowRectangles(),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700269 GrWindowRectsState::Mode::kExclusive);
270 }
271
csmartdaltond211e782016-08-15 11:17:19 -0700272 if (reducedClip.elements().isEmpty()) {
273 return InitialState::kAllIn == reducedClip.initialState();
274 }
275
csmartdalton3affdc12016-10-28 12:01:10 -0700276#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700277 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500278 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
279 renderTargetContext->height());
Brian Salomon9a767722017-03-13 17:57:28 -0400280 const SkIRect& clipIBounds = reducedClip.ibounds();
csmartdalton3affdc12016-10-28 12:01:10 -0700281 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
282#endif
csmartdaltond211e782016-08-15 11:17:19 -0700283
Eric Karl5c779752017-05-08 12:02:07 -0700284 bool avoidStencilBuffers = context->caps()->avoidStencilBuffers();
285
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000286 // An element count of 4 was chosen because of the common pattern in Blink of:
287 // isect RR
288 // diff RR
289 // isect convex_poly
290 // isect convex_poly
291 // when drawing rounded div borders. This could probably be tuned based on a
292 // configuration's relative costs of switching RTs to generate a mask vs
293 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700294 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800295 // When there are multiple samples we want to do per-sample clipping, not compute a
296 // fractional pixel coverage.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400297 bool disallowAnalyticAA =
298 GrFSAAType::kNone != renderTargetContext->fsaaType() && !avoidStencilBuffers;
Brian Osman11052242016-10-27 14:47:55 -0400299 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700300 // With a single color sample, any coverage info is lost from color once it hits the
301 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
302 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700303 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700304 }
bungeman06ca8ec2016-06-09 08:01:03 -0700305 sk_sp<GrFragmentProcessor> clipFP;
Eric Karl5c779752017-05-08 12:02:07 -0700306 if ((reducedClip.requiresAA() || avoidStencilBuffers) &&
Brian Salomon9a767722017-03-13 17:57:28 -0400307 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA, devBounds,
308 &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700309 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000310 return true;
311 }
312 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000313
cdaltonede75742015-11-11 15:27:57 -0800314 // If the stencil buffer is multisampled we can use it to do everything.
Brian Salomon7c8460e2017-05-12 11:36:10 -0400315 if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.requiresAA()) ||
Eric Karl5c779752017-05-08 12:02:07 -0700316 avoidStencilBuffers) {
Robert Phillips875218e2017-02-24 08:37:13 -0500317 sk_sp<GrTextureProxy> result;
Brian Osman11052242016-10-27 14:47:55 -0400318 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000319 // The clip geometry is complex enough that it will be more efficient to create it
320 // entirely in software
Brian Salomon19f0ed52017-01-06 13:54:58 -0500321 result = this->createSoftwareClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000322 } else {
Brian Salomon19f0ed52017-01-06 13:54:58 -0500323 result = this->createAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000324 }
325
bsalomon49f085d2014-09-05 13:34:00 -0700326 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 // The mask's top left coord should be pinned to the rounded-out top left corner of
Brian Salomon9a767722017-03-13 17:57:28 -0400328 // the clip's device space bounds.
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400329 out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.ibounds()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000330 return true;
331 }
Eric Karl5c779752017-05-08 12:02:07 -0700332
333 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
334 // unless stencils are disallowed.
335 if (context->caps()->avoidStencilBuffers()) {
336 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. Clip will be ignored.\n");
337 return false;
338 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000339 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340
Robert Phillipsec2249f2016-11-09 08:54:35 -0500341 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500342 if (!rt) {
343 return true;
344 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500345
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000346 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700347 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700348 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
349 return true;
350 }
351
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700352 // This relies on the property that a reduced sub-rect of the last clip will contain all the
353 // relevant window rectangles that were in the last clip. This subtle requirement will go away
354 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700355 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
Brian Salomon9a767722017-03-13 17:57:28 -0400356 reducedClip.ibounds())) {
357 reducedClip.drawStencilClipMask(context, renderTargetContext);
358 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds());
csmartdaltonbde96c62016-08-31 12:54:46 -0700359 }
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400360 out->addStencilClip(reducedClip.elementsGenID());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000361 return true;
362}
363
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000364////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700365// Create a 8-bit clip mask in alpha
366
Robert Phillips806be2d2017-06-28 15:23:59 -0400367static void create_clip_mask_key(uint32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
bsalomon473addf2015-10-02 07:49:05 -0700368 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon19f0ed52017-01-06 13:54:58 -0500369 GrUniqueKey::Builder builder(key, kDomain, 3, GrClipStackClip::kMaskTestTag);
bsalomon473addf2015-10-02 07:49:05 -0700370 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700371 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
Brian Salomon9a767722017-03-13 17:57:28 -0400372 // sometimes result in negative coordinates from device space.
csmartdalton3affdc12016-10-28 12:01:10 -0700373 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
374 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700375}
376
Robert Phillips806be2d2017-06-28 15:23:59 -0400377static void add_invalidate_on_pop_message(const SkClipStack& stack, uint32_t clipGenID,
Brian Salomon19f0ed52017-01-06 13:54:58 -0500378 const GrUniqueKey& clipMaskKey) {
379 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
380 while (const Element* element = iter.prev()) {
381 if (element->getGenID() == clipGenID) {
382 std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
383 new GrUniqueKeyInvalidatedMessage(clipMaskKey));
384 element->addResourceInvalidationMessage(std::move(msg));
385 return;
386 }
387 }
388 SkDEBUGFAIL("Gen ID was not found in stack.");
389}
390
Robert Phillips875218e2017-02-24 08:37:13 -0500391sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
392 const GrReducedClip& reducedClip) const {
Brian Osman32342f02017-03-04 08:12:46 -0500393 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700394 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500395 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500396
Robert Phillips6af2a662017-07-25 14:11:29 +0000397 sk_sp<GrTextureProxy> proxy(resourceProvider->findProxyByUniqueKey(key));
Robert Phillipsd3749482017-03-14 09:17:43 -0400398 if (proxy) {
399 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000400 }
401
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400402 sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400403 SkBackingFit::kApprox,
404 reducedClip.width(),
405 reducedClip.height(),
406 kAlpha_8_GrPixelConfig,
407 nullptr));
408 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800409 return nullptr;
410 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000411
Brian Osman11052242016-10-27 14:47:55 -0400412 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700413 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414 }
415
Robert Phillips875218e2017-02-24 08:37:13 -0500416 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
417 if (!result) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500418 return nullptr;
419 }
420
Robert Phillips019ff272017-07-24 14:47:57 -0400421 SkASSERT(result->origin() == kBottomLeft_GrSurfaceOrigin);
Robert Phillipsd3749482017-03-14 09:17:43 -0400422 resourceProvider->assignUniqueKeyToProxy(key, result.get());
423 // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
Brian Salomon19f0ed52017-01-06 13:54:58 -0500424 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500425
426 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427}
428
Robert Phillips875218e2017-02-24 08:37:13 -0500429sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
430 GrContext* context,
431 const GrReducedClip& reducedClip) const {
bsalomon473addf2015-10-02 07:49:05 -0700432 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500433 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500434
Robert Phillips6af2a662017-07-25 14:11:29 +0000435 sk_sp<GrTextureProxy> proxy(context->resourceProvider()->findProxyByUniqueKey(key));
Robert Phillipsd3749482017-03-14 09:17:43 -0400436 if (proxy) {
437 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000438 }
439
Brian Salomon9a767722017-03-13 17:57:28 -0400440 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
441 // left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700442 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500444 GrSWMaskHelper helper;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000445
joshualitt8059eb92014-12-29 15:10:07 -0800446 // Set the matrix so that rendered clip elements are transformed to mask space from clip
447 // space.
448 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700449 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800450
csmartdalton3affdc12016-10-28 12:01:10 -0700451 if (!helper.init(maskSpaceIBounds, &translate)) {
452 return nullptr;
453 }
csmartdaltond211e782016-08-15 11:17:19 -0700454 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000455
csmartdalton77f2fae2016-08-08 09:55:06 -0700456 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000457 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500458 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500459 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000460
Mike Reedc1f77742016-12-09 09:00:50 -0500461 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000462 // Intersect and reverse difference require modifying pixels outside of the geometry
463 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
464 // but leave the pixels inside the geometry alone. For reverse difference we invert all
465 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500466 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700467 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000468 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500469 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000470 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000471 SkPath clipPath;
472 element->asPath(&clipPath);
473 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700474 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000476 continue;
477 }
478
479 // The other ops (union, xor, diff) only affect pixels inside
480 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000481 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500482 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000483 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000484 SkPath path;
485 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700486 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500487 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000488 }
489 }
490
Robert Phillipsd3749482017-03-14 09:17:43 -0400491 sk_sp<GrTextureProxy> result(helper.toTextureProxy(context, SkBackingFit::kApprox));
robertphillips391395d2016-03-02 09:26:36 -0800492
Robert Phillips019ff272017-07-24 14:47:57 -0400493 SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillipsd3749482017-03-14 09:17:43 -0400494 context->resourceProvider()->assignUniqueKeyToProxy(key, result.get());
495 // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
Brian Salomon19f0ed52017-01-06 13:54:58 -0500496 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500497 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000498}