blob: e24e63565b254b1fab6ca773a56e5517be49324a [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"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050019#include "GrTextureProxy.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080020#include "effects/GrConvexPolyEffect.h"
21#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrTextureDomain.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050023#include "SkClipOpPriv.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000024
bsalomon@google.com8182fa02012-12-04 14:06:06 +000025typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070026typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070027typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
robertphillips976f5f02016-06-03 10:59:20 -070029static const int kMaxAnalyticElements = 4;
Brian Salomon19f0ed52017-01-06 13:54:58 -050030const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
robertphillips976f5f02016-06-03 10:59:20 -070031
csmartdaltonc6f411e2016-08-05 22:32:12 -070032bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070033 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070034 return true;
35 }
Brian Salomon9a767722017-03-13 17:57:28 -040036 return fStack->quickContains(rect);
csmartdaltonc6f411e2016-08-05 22:32:12 -070037}
38
bsalomon7f0d9f32016-08-15 14:49:10 -070039bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070040 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070041 return true;
42 }
Brian Salomon9a767722017-03-13 17:57:28 -040043 return fStack->quickContains(rrect);
bsalomon7f0d9f32016-08-15 14:49:10 -070044}
45
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050046bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070047 if (!fStack) {
48 return false;
49 }
50 const SkRect* rtBounds = &origRTBounds;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050051 bool isAA;
52 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
53 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070054 return true;
55 }
56 return false;
57}
58
csmartdaltonc6f411e2016-08-05 22:32:12 -070059void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
60 bool* isIntersectionOfRects) const {
61 if (!fStack) {
62 devResult->setXYWH(0, 0, width, height);
63 if (isIntersectionOfRects) {
64 *isIntersectionOfRects = true;
65 }
66 return;
67 }
68 SkRect devBounds;
Brian Salomon9a767722017-03-13 17:57:28 -040069 fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
csmartdaltonc6f411e2016-08-05 22:32:12 -070070 devBounds.roundOut(devResult);
71}
72
bsalomon@google.com51a62862012-11-26 21:19:43 +000073////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040074// set up the draw state to enable the aa clipping mask.
Robert Phillips296b1cc2017-03-15 10:42:12 -040075static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrResourceProvider* resourceProvider,
Robert Phillips875218e2017-02-24 08:37:13 -050076 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 Phillips296b1cc2017-03-15 10:42:12 -040079 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(resourceProvider,
80 std::move(mask), domainTexels,
Brian Salomon2ebd0c82016-10-03 17:15:28 -040081 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000082}
83
robertphillips3f7357f2015-10-27 07:17:33 -070084// Does the path in 'element' require SW rendering? If so, return true (and,
85// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
86// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070087bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
88 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040089 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070090 const SkMatrix& viewMatrix,
91 const Element* element,
92 GrPathRenderer** prOut,
93 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070094 if (Element::kRect_Type == element->getType()) {
95 // rects can always be drawn directly w/o using the software path
96 // TODO: skip rrects once we're drawing them directly.
97 if (prOut) {
98 *prOut = nullptr;
99 }
100 return false;
101 } else {
102 // We shouldn't get here with an empty clip element.
103 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700104
robertphillips3f7357f2015-10-27 07:17:33 -0700105 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
106 SkPath path;
107 element->asPath(&path);
108 if (path.isInverseFillType()) {
109 path.toggleInverseFillType();
110 }
halcanary9d524f22016-03-29 09:03:52 -0700111
Brian Salomon82125e92016-12-10 09:35:48 -0500112 GrPathRendererChain::DrawType type =
113 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
114 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700115
bsalomon8acedde2016-06-24 10:42:16 -0700116 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700117 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700118 canDrawArgs.fCaps = context->caps();
robertphillips68737822015-10-29 12:12:21 -0700119 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700120 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500121 if (!element->isAA()) {
122 canDrawArgs.fAAType = GrAAType::kNone;
123 } else if (renderTargetContext->isUnifiedMultisampled()) {
124 canDrawArgs.fAAType = GrAAType::kMSAA;
125 } else if (renderTargetContext->isStencilBufferMultisampled()){
126 canDrawArgs.fAAType = GrAAType::kMixedSamples;
127 } else {
128 canDrawArgs.fAAType = GrAAType::kCoverage;
129 }
cdalton93a379b2016-05-11 13:58:08 -0700130 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700131
robertphillips3f7357f2015-10-27 07:17:33 -0700132 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700133 GrPathRenderer* pr =
134 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700135 if (prOut) {
136 *prOut = pr;
137 }
138 return SkToBool(!pr);
139 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000140}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000141
robertphillips@google.comfa662942012-05-17 12:20:22 +0000142/*
143 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
144 * will be used on any element. If so, it returns true to indicate that the
145 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
146 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700147bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
148 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400149 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700150 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000151 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000152 // a clip gets complex enough it can just be done in SW regardless
153 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000154
Eric Karl5c779752017-05-08 12:02:07 -0700155 // If we're avoiding stencils, always use SW:
156 if (context->caps()->avoidStencilBuffers())
157 return true;
158
joshualitt8059eb92014-12-29 15:10:07 -0800159 // Set the matrix so that rendered clip elements are transformed to mask space from clip
160 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700161 SkMatrix translate;
162 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800163
csmartdaltonbde96c62016-08-31 12:54:46 -0700164 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000165 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700166
Mike Reedc1f77742016-12-09 09:00:50 -0500167 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700168 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700169 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500170 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700171
robertphillips59cf61a2016-07-13 09:18:21 -0700172 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400173 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700174 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000175 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000176 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000177 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000178}
179
csmartdalton77f2fae2016-08-08 09:55:06 -0700180static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700181 bool abortIfAA,
Brian Salomon9a767722017-03-13 17:57:28 -0400182 const SkRect& drawDevBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700183 sk_sp<GrFragmentProcessor>* resultFP) {
bsalomon0ba8c242015-10-07 09:20:28 -0700184 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700185 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700186 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700187 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500188 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 bool invert;
190 bool skip = false;
191 switch (op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500192 case kReplace_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000193 SkASSERT(iter.get() == elements.head());
194 // Fallthrough, handled same as intersect.
Mike Reedebfce6d2016-12-12 10:02:12 -0500195 case kIntersect_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 invert = false;
Brian Salomon9a767722017-03-13 17:57:28 -0400197 if (iter.get()->contains(drawDevBounds)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 skip = true;
199 }
200 break;
Mike Reedebfce6d2016-12-12 10:02:12 -0500201 case kDifference_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 invert = true;
203 // We don't currently have a cheap test for whether a rect is fully outside an
204 // element's primitive, so don't attempt to set skip.
205 break;
206 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700207 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700210 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800211 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700212 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700213 return false;
bsalomona912dde2015-10-14 15:01:50 -0700214 }
joshualittb0a8a372014-09-23 09:50:21 -0700215 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700216 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700218 edgeType =
219 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000220 }
bsalomona912dde2015-10-14 15:01:50 -0700221
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 switch (iter.get()->getType()) {
223 case SkClipStack::Element::kPath_Type:
Brian Salomon9a767722017-03-13 17:57:28 -0400224 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225 break;
226 case SkClipStack::Element::kRRect_Type: {
Brian Salomon9a767722017-03-13 17:57:28 -0400227 fps.emplace_back(GrRRectEffect::Make(edgeType, iter.get()->getRRect()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 break;
229 }
230 case SkClipStack::Element::kRect_Type: {
Brian Salomon9a767722017-03-13 17:57:28 -0400231 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getRect()));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000232 break;
233 }
234 default:
235 break;
236 }
bungeman06ca8ec2016-06-09 08:01:03 -0700237 if (!fps.back()) {
238 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000239 }
240 }
mtklein217daa72014-07-02 12:55:21 -0700241 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000242 }
243
bsalomon0b5b6b22015-10-14 08:31:34 -0700244 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700245 if (fps.count()) {
246 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000247 }
bungeman06ca8ec2016-06-09 08:01:03 -0700248 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000249}
250
robertphillips@google.comf294b772012-04-27 14:29:26 +0000251////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000252// sort out what kind of clip mask needs to be created: alpha, stencil,
253// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400254bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
Brian Salomon97180af2017-03-14 13:42:58 -0400255 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
256 SkRect* bounds) const {
Brian Salomon97180af2017-03-14 13:42:58 -0400257 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
258 if (!devBounds.intersect(*bounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700259 return false;
260 }
261
Brian Salomon510dd422017-03-16 12:15:22 -0400262 if (!fStack || fStack->isWideOpen()) {
263 return true;
264 }
265
Brian Salomon9a767722017-03-13 17:57:28 -0400266 const GrReducedClip reducedClip(*fStack, devBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500267 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000268
Brian Salomon9a767722017-03-13 17:57:28 -0400269 if (reducedClip.hasIBounds() && !GrClip::IsInsideClip(reducedClip.ibounds(), devBounds)) {
Brian Salomon97180af2017-03-14 13:42:58 -0400270 out->addScissor(reducedClip.ibounds(), bounds);
cdalton846c0512016-05-13 10:25:00 -0700271 }
cdalton93a379b2016-05-11 13:58:08 -0700272
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700273 if (!reducedClip.windowRectangles().empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400274 out->addWindowRectangles(reducedClip.windowRectangles(),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700275 GrWindowRectsState::Mode::kExclusive);
276 }
277
csmartdaltond211e782016-08-15 11:17:19 -0700278 if (reducedClip.elements().isEmpty()) {
279 return InitialState::kAllIn == reducedClip.initialState();
280 }
281
csmartdalton3affdc12016-10-28 12:01:10 -0700282#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700283 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500284 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
285 renderTargetContext->height());
Brian Salomon9a767722017-03-13 17:57:28 -0400286 const SkIRect& clipIBounds = reducedClip.ibounds();
csmartdalton3affdc12016-10-28 12:01:10 -0700287 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
288#endif
csmartdaltond211e782016-08-15 11:17:19 -0700289
Eric Karl5c779752017-05-08 12:02:07 -0700290 bool avoidStencilBuffers = context->caps()->avoidStencilBuffers();
291
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000292 // An element count of 4 was chosen because of the common pattern in Blink of:
293 // isect RR
294 // diff RR
295 // isect convex_poly
296 // isect convex_poly
297 // when drawing rounded div borders. This could probably be tuned based on a
298 // configuration's relative costs of switching RTs to generate a mask vs
299 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700300 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800301 // When there are multiple samples we want to do per-sample clipping, not compute a
302 // fractional pixel coverage.
Eric Karl5c779752017-05-08 12:02:07 -0700303 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled() &&
304 !avoidStencilBuffers;
Brian Osman11052242016-10-27 14:47:55 -0400305 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700306 // With a single color sample, any coverage info is lost from color once it hits the
307 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
308 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700309 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700310 }
bungeman06ca8ec2016-06-09 08:01:03 -0700311 sk_sp<GrFragmentProcessor> clipFP;
Eric Karl5c779752017-05-08 12:02:07 -0700312 if ((reducedClip.requiresAA() || avoidStencilBuffers) &&
Brian Salomon9a767722017-03-13 17:57:28 -0400313 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA, devBounds,
314 &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700315 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000316 return true;
317 }
318 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000319
cdaltonede75742015-11-11 15:27:57 -0800320 // If the stencil buffer is multisampled we can use it to do everything.
Eric Karl5c779752017-05-08 12:02:07 -0700321 if ((!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) ||
322 avoidStencilBuffers) {
Robert Phillips875218e2017-02-24 08:37:13 -0500323 sk_sp<GrTextureProxy> result;
Brian Osman11052242016-10-27 14:47:55 -0400324 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 // The clip geometry is complex enough that it will be more efficient to create it
326 // entirely in software
Brian Salomon19f0ed52017-01-06 13:54:58 -0500327 result = this->createSoftwareClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 } else {
Brian Salomon19f0ed52017-01-06 13:54:58 -0500329 result = this->createAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000330 }
331
bsalomon49f085d2014-09-05 13:34:00 -0700332 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333 // The mask's top left coord should be pinned to the rounded-out top left corner of
Brian Salomon9a767722017-03-13 17:57:28 -0400334 // the clip's device space bounds.
Robert Phillips296b1cc2017-03-15 10:42:12 -0400335 out->addCoverageFP(create_fp_for_mask(context->resourceProvider(), std::move(result),
Brian Salomon9a767722017-03-13 17:57:28 -0400336 reducedClip.ibounds()));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000337 return true;
338 }
Eric Karl5c779752017-05-08 12:02:07 -0700339
340 // If alpha or software clip mask creation fails, fall through to the stencil code paths,
341 // unless stencils are disallowed.
342 if (context->caps()->avoidStencilBuffers()) {
343 SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. Clip will be ignored.\n");
344 return false;
345 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000347
Robert Phillipsec2249f2016-11-09 08:54:35 -0500348 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500349 if (!rt) {
350 return true;
351 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500352
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000353 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700354 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700355 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
356 return true;
357 }
358
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700359 // This relies on the property that a reduced sub-rect of the last clip will contain all the
360 // relevant window rectangles that were in the last clip. This subtle requirement will go away
361 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700362 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
Brian Salomon9a767722017-03-13 17:57:28 -0400363 reducedClip.ibounds())) {
364 reducedClip.drawStencilClipMask(context, renderTargetContext);
365 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds());
csmartdaltonbde96c62016-08-31 12:54:46 -0700366 }
csmartdaltond211e782016-08-15 11:17:19 -0700367 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368 return true;
369}
370
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000371////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700372// Create a 8-bit clip mask in alpha
373
Brian Salomon19f0ed52017-01-06 13:54:58 -0500374static void create_clip_mask_key(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
bsalomon473addf2015-10-02 07:49:05 -0700375 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon19f0ed52017-01-06 13:54:58 -0500376 GrUniqueKey::Builder builder(key, kDomain, 3, GrClipStackClip::kMaskTestTag);
bsalomon473addf2015-10-02 07:49:05 -0700377 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700378 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
Brian Salomon9a767722017-03-13 17:57:28 -0400379 // sometimes result in negative coordinates from device space.
csmartdalton3affdc12016-10-28 12:01:10 -0700380 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
381 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700382}
383
Brian Salomon19f0ed52017-01-06 13:54:58 -0500384static void add_invalidate_on_pop_message(const SkClipStack& stack, int32_t clipGenID,
385 const GrUniqueKey& clipMaskKey) {
386 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
387 while (const Element* element = iter.prev()) {
388 if (element->getGenID() == clipGenID) {
389 std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
390 new GrUniqueKeyInvalidatedMessage(clipMaskKey));
391 element->addResourceInvalidationMessage(std::move(msg));
392 return;
393 }
394 }
395 SkDEBUGFAIL("Gen ID was not found in stack.");
396}
397
Robert Phillips875218e2017-02-24 08:37:13 -0500398sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
399 const GrReducedClip& reducedClip) const {
Brian Osman32342f02017-03-04 08:12:46 -0500400 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700401 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500402 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500403
Robert Phillipsd3749482017-03-14 09:17:43 -0400404 sk_sp<GrTextureProxy> proxy(resourceProvider->findProxyByUniqueKey(key));
405 if (proxy) {
406 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000407 }
408
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400409 sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400410 SkBackingFit::kApprox,
411 reducedClip.width(),
412 reducedClip.height(),
413 kAlpha_8_GrPixelConfig,
414 nullptr));
415 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800416 return nullptr;
417 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000418
Brian Osman11052242016-10-27 14:47:55 -0400419 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700420 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421 }
422
Robert Phillips875218e2017-02-24 08:37:13 -0500423 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
424 if (!result) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500425 return nullptr;
426 }
427
Robert Phillipsd3749482017-03-14 09:17:43 -0400428 resourceProvider->assignUniqueKeyToProxy(key, result.get());
429 // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
Brian Salomon19f0ed52017-01-06 13:54:58 -0500430 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500431
432 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433}
434
Robert Phillips875218e2017-02-24 08:37:13 -0500435sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
436 GrContext* context,
437 const GrReducedClip& reducedClip) const {
bsalomon473addf2015-10-02 07:49:05 -0700438 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500439 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500440
Robert Phillipsd3749482017-03-14 09:17:43 -0400441 sk_sp<GrTextureProxy> proxy(context->resourceProvider()->findProxyByUniqueKey(key));
442 if (proxy) {
443 return proxy;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000444 }
445
Brian Salomon9a767722017-03-13 17:57:28 -0400446 // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
447 // left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700448 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000449
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500450 GrSWMaskHelper helper;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000451
joshualitt8059eb92014-12-29 15:10:07 -0800452 // Set the matrix so that rendered clip elements are transformed to mask space from clip
453 // space.
454 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700455 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800456
csmartdalton3affdc12016-10-28 12:01:10 -0700457 if (!helper.init(maskSpaceIBounds, &translate)) {
458 return nullptr;
459 }
csmartdaltond211e782016-08-15 11:17:19 -0700460 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000461
csmartdalton77f2fae2016-08-08 09:55:06 -0700462 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500464 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500465 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000466
Mike Reedc1f77742016-12-09 09:00:50 -0500467 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000468 // Intersect and reverse difference require modifying pixels outside of the geometry
469 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
470 // but leave the pixels inside the geometry alone. For reverse difference we invert all
471 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500472 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700473 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000474 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500475 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000476 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000477 SkPath clipPath;
478 element->asPath(&clipPath);
479 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700480 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500481 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000482 continue;
483 }
484
485 // The other ops (union, xor, diff) only affect pixels inside
486 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000487 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500488 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000489 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000490 SkPath path;
491 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700492 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500493 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494 }
495 }
496
Robert Phillipsd3749482017-03-14 09:17:43 -0400497 sk_sp<GrTextureProxy> result(helper.toTextureProxy(context, SkBackingFit::kApprox));
robertphillips391395d2016-03-02 09:26:36 -0800498
Robert Phillipsd3749482017-03-14 09:17:43 -0400499 context->resourceProvider()->assignUniqueKeyToProxy(key, result.get());
500 // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
Brian Salomon19f0ed52017-01-06 13:54:58 -0500501 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500502 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000503}