blob: a0682f444d452d4177c55b0b99d203747d8575ac [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 }
36 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
37 SkIntToScalar(fOrigin.y())));
38}
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 }
44 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
45 SkIntToScalar(fOrigin.fY)));
46}
47
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050048bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
bsalomoncb31e512016-08-26 10:48:19 -070049 if (!fStack) {
50 return false;
51 }
52 const SkRect* rtBounds = &origRTBounds;
53 SkRect tempRTBounds;
54 bool origin = fOrigin.fX || fOrigin.fY;
55 if (origin) {
56 tempRTBounds = origRTBounds;
57 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
58 rtBounds = &tempRTBounds;
59 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050060 bool isAA;
61 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
62 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070063 if (origin) {
64 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
65 }
66 return true;
67 }
68 return false;
69}
70
csmartdaltonc6f411e2016-08-05 22:32:12 -070071void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
72 bool* isIntersectionOfRects) const {
73 if (!fStack) {
74 devResult->setXYWH(0, 0, width, height);
75 if (isIntersectionOfRects) {
76 *isIntersectionOfRects = true;
77 }
78 return;
79 }
80 SkRect devBounds;
81 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
82 isIntersectionOfRects);
83 devBounds.roundOut(devResult);
84}
85
bsalomon@google.com51a62862012-11-26 21:19:43 +000086////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040087// set up the draw state to enable the aa clipping mask.
Robert Phillips875218e2017-02-24 08:37:13 -050088static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrContext* context,
89 sk_sp<GrTextureProxy> mask,
bungeman06ca8ec2016-06-09 08:01:03 -070090 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000091 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Robert Phillips875218e2017-02-24 08:37:13 -050092 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(context, std::move(mask), domainTexels,
Brian Salomon2ebd0c82016-10-03 17:15:28 -040093 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000094}
95
robertphillips3f7357f2015-10-27 07:17:33 -070096// Does the path in 'element' require SW rendering? If so, return true (and,
97// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
98// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070099bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
100 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400101 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700102 const SkMatrix& viewMatrix,
103 const Element* element,
104 GrPathRenderer** prOut,
105 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700106 if (Element::kRect_Type == element->getType()) {
107 // rects can always be drawn directly w/o using the software path
108 // TODO: skip rrects once we're drawing them directly.
109 if (prOut) {
110 *prOut = nullptr;
111 }
112 return false;
113 } else {
114 // We shouldn't get here with an empty clip element.
115 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700116
robertphillips3f7357f2015-10-27 07:17:33 -0700117 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
118 SkPath path;
119 element->asPath(&path);
120 if (path.isInverseFillType()) {
121 path.toggleInverseFillType();
122 }
halcanary9d524f22016-03-29 09:03:52 -0700123
Brian Salomon82125e92016-12-10 09:35:48 -0500124 GrPathRendererChain::DrawType type =
125 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
126 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700127
bsalomon8acedde2016-06-24 10:42:16 -0700128 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700129 GrPathRenderer::CanDrawPathArgs canDrawArgs;
130 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
131 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700132 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500133 if (!element->isAA()) {
134 canDrawArgs.fAAType = GrAAType::kNone;
135 } else if (renderTargetContext->isUnifiedMultisampled()) {
136 canDrawArgs.fAAType = GrAAType::kMSAA;
137 } else if (renderTargetContext->isStencilBufferMultisampled()){
138 canDrawArgs.fAAType = GrAAType::kMixedSamples;
139 } else {
140 canDrawArgs.fAAType = GrAAType::kCoverage;
141 }
cdalton93a379b2016-05-11 13:58:08 -0700142 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700143
robertphillips3f7357f2015-10-27 07:17:33 -0700144 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700145 GrPathRenderer* pr =
146 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700147 if (prOut) {
148 *prOut = pr;
149 }
150 return SkToBool(!pr);
151 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000152}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000153
robertphillips@google.comfa662942012-05-17 12:20:22 +0000154/*
155 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
156 * will be used on any element. If so, it returns true to indicate that the
157 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
158 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700159bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
160 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400161 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700162 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000163 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000164 // a clip gets complex enough it can just be done in SW regardless
165 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000166
joshualitt8059eb92014-12-29 15:10:07 -0800167 // Set the matrix so that rendered clip elements are transformed to mask space from clip
168 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700169 SkMatrix translate;
170 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800171
csmartdaltonbde96c62016-08-31 12:54:46 -0700172 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000173 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700174
Mike Reedc1f77742016-12-09 09:00:50 -0500175 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700176 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700177 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500178 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700179
robertphillips59cf61a2016-07-13 09:18:21 -0700180 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400181 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700182 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000183 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000184 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000185 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000186}
187
csmartdalton77f2fae2016-08-08 09:55:06 -0700188static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700189 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700190 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700191 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700192 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000193 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700194 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700195 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700196 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700197 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700198 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500199 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000200 bool invert;
201 bool skip = false;
202 switch (op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500203 case kReplace_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 SkASSERT(iter.get() == elements.head());
205 // Fallthrough, handled same as intersect.
Mike Reedebfce6d2016-12-12 10:02:12 -0500206 case kIntersect_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700208 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 skip = true;
210 }
211 break;
Mike Reedebfce6d2016-12-12 10:02:12 -0500212 case kDifference_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 invert = true;
214 // We don't currently have a cheap test for whether a rect is fully outside an
215 // element's primitive, so don't attempt to set skip.
216 break;
217 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700218 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000220 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700221 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800222 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700223 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700224 return false;
bsalomona912dde2015-10-14 15:01:50 -0700225 }
joshualittb0a8a372014-09-23 09:50:21 -0700226 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700227 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700229 edgeType =
230 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 }
bsalomona912dde2015-10-14 15:01:50 -0700232
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 switch (iter.get()->getType()) {
234 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700235 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
236 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000237 break;
238 case SkClipStack::Element::kRRect_Type: {
239 SkRRect rrect = iter.get()->getRRect();
240 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700241 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000242 break;
243 }
244 case SkClipStack::Element::kRect_Type: {
245 SkRect rect = iter.get()->getRect();
246 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700247 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 break;
249 }
250 default:
251 break;
252 }
bungeman06ca8ec2016-06-09 08:01:03 -0700253 if (!fps.back()) {
254 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000255 }
256 }
mtklein217daa72014-07-02 12:55:21 -0700257 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000258 }
259
bsalomon0b5b6b22015-10-14 08:31:34 -0700260 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700261 if (fps.count()) {
262 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263 }
bungeman06ca8ec2016-06-09 08:01:03 -0700264 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000265}
266
robertphillips@google.comf294b772012-04-27 14:29:26 +0000267////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000268// sort out what kind of clip mask needs to be created: alpha, stencil,
269// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400270bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
271 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700272 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700273 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700274 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000275
Robert Phillips784b7bf2016-12-09 13:35:02 -0500276 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(),
277 renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700278 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700279 return false;
280 }
281
csmartdaltonc6f411e2016-08-05 22:32:12 -0700282 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
283 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700284
csmartdalton77f2fae2016-08-08 09:55:06 -0700285 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700286 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500287 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000288
csmartdaltond211e782016-08-15 11:17:19 -0700289 if (reducedClip.hasIBounds() &&
290 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
291 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
292 scissorSpaceIBounds.offset(-fOrigin);
293 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700294 }
cdalton93a379b2016-05-11 13:58:08 -0700295
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700296 if (!reducedClip.windowRectangles().empty()) {
297 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
298 GrWindowRectsState::Mode::kExclusive);
299 }
300
csmartdaltond211e782016-08-15 11:17:19 -0700301 if (reducedClip.elements().isEmpty()) {
302 return InitialState::kAllIn == reducedClip.initialState();
303 }
304
csmartdalton3affdc12016-10-28 12:01:10 -0700305#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700306 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500307 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
308 renderTargetContext->height());
csmartdalton3affdc12016-10-28 12:01:10 -0700309 SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y());
310 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
311#endif
csmartdaltond211e782016-08-15 11:17:19 -0700312
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000313 // An element count of 4 was chosen because of the common pattern in Blink of:
314 // isect RR
315 // diff RR
316 // isect convex_poly
317 // isect convex_poly
318 // when drawing rounded div borders. This could probably be tuned based on a
319 // configuration's relative costs of switching RTs to generate a mask vs
320 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700321 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800322 // When there are multiple samples we want to do per-sample clipping, not compute a
323 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400324 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
325 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700326 // With a single color sample, any coverage info is lost from color once it hits the
327 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
328 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700329 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700330 }
bungeman06ca8ec2016-06-09 08:01:03 -0700331 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700332 if (reducedClip.requiresAA() &&
333 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
334 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700335 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000336 return true;
337 }
338 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000339
cdaltonede75742015-11-11 15:27:57 -0800340 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400341 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
Robert Phillips875218e2017-02-24 08:37:13 -0500342 sk_sp<GrTextureProxy> result;
Brian Osman11052242016-10-27 14:47:55 -0400343 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // The clip geometry is complex enough that it will be more efficient to create it
345 // entirely in software
Brian Salomon19f0ed52017-01-06 13:54:58 -0500346 result = this->createSoftwareClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 } else {
Brian Salomon19f0ed52017-01-06 13:54:58 -0500348 result = this->createAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349 }
350
bsalomon49f085d2014-09-05 13:34:00 -0700351 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352 // The mask's top left coord should be pinned to the rounded-out top left corner of
353 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700354 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700355 rtSpaceMaskBounds.offset(-fOrigin);
Robert Phillips875218e2017-02-24 08:37:13 -0500356 out->addCoverageFP(create_fp_for_mask(context, std::move(result), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357 return true;
358 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000359 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
Robert Phillipsec2249f2016-11-09 08:54:35 -0500362 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500363 if (!rt) {
364 return true;
365 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500366
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700368 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700369 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
370 return true;
371 }
372
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700373 // This relies on the property that a reduced sub-rect of the last clip will contain all the
374 // relevant window rectangles that were in the last clip. This subtle requirement will go away
375 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700376 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
377 reducedClip.ibounds(), fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400378 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdalton7cdda992016-11-01 07:03:03 -0700379 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
380 fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700381 }
csmartdaltond211e782016-08-15 11:17:19 -0700382 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000383 return true;
384}
385
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000386////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700387// Create a 8-bit clip mask in alpha
388
Brian Salomon19f0ed52017-01-06 13:54:58 -0500389static void create_clip_mask_key(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
bsalomon473addf2015-10-02 07:49:05 -0700390 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Salomon19f0ed52017-01-06 13:54:58 -0500391 GrUniqueKey::Builder builder(key, kDomain, 3, GrClipStackClip::kMaskTestTag);
bsalomon473addf2015-10-02 07:49:05 -0700392 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700393 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
394 // sometimes result in negative coordinates from clip space.
395 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
396 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700397}
398
Brian Salomon19f0ed52017-01-06 13:54:58 -0500399static void add_invalidate_on_pop_message(const SkClipStack& stack, int32_t clipGenID,
400 const GrUniqueKey& clipMaskKey) {
401 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
402 while (const Element* element = iter.prev()) {
403 if (element->getGenID() == clipGenID) {
404 std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
405 new GrUniqueKeyInvalidatedMessage(clipMaskKey));
406 element->addResourceInvalidationMessage(std::move(msg));
407 return;
408 }
409 }
410 SkDEBUGFAIL("Gen ID was not found in stack.");
411}
412
Robert Phillips875218e2017-02-24 08:37:13 -0500413// MDB TODO (caching): this side-steps the issue of texture proxies cached by unique ID
414sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
415 const GrReducedClip& reducedClip) const {
Brian Osman32342f02017-03-04 08:12:46 -0500416 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700417 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500418 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500419
Brian Osman32342f02017-03-04 08:12:46 -0500420 sk_sp<GrTexture> texture(resourceProvider->findAndRefTextureByUniqueKey(key));
Robert Phillips875218e2017-02-24 08:37:13 -0500421 if (texture) {
422 return GrSurfaceProxy::MakeWrapped(std::move(texture));
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000423 }
424
Brian Osman11052242016-10-27 14:47:55 -0400425 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
426 SkBackingFit::kApprox,
427 reducedClip.width(),
428 reducedClip.height(),
429 kAlpha_8_GrPixelConfig,
430 nullptr));
431 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800432 return nullptr;
433 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434
Brian Osman11052242016-10-27 14:47:55 -0400435 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700436 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 }
438
Robert Phillips875218e2017-02-24 08:37:13 -0500439 sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
440 if (!result) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500441 return nullptr;
442 }
443
Brian Osman32342f02017-03-04 08:12:46 -0500444 GrTexture* tex = result->instantiate(context->resourceProvider());
Robert Phillips875218e2017-02-24 08:37:13 -0500445 if (!tex) {
446 return nullptr;
447 }
448
Brian Osman32342f02017-03-04 08:12:46 -0500449 context->resourceProvider()->assignUniqueKeyToTexture(key, tex);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500450 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500451
452 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453}
454
Robert Phillips875218e2017-02-24 08:37:13 -0500455// MDB TODO (caching): This side-steps the caching of texture proxies by unique ID
456sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
457 GrContext* context,
458 const GrReducedClip& reducedClip) const {
bsalomon473addf2015-10-02 07:49:05 -0700459 GrUniqueKey key;
Brian Salomon19f0ed52017-01-06 13:54:58 -0500460 create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillips875218e2017-02-24 08:37:13 -0500461
Brian Osman32342f02017-03-04 08:12:46 -0500462 sk_sp<GrTexture> texture(context->resourceProvider()->findAndRefTextureByUniqueKey(key));
Robert Phillips875218e2017-02-24 08:37:13 -0500463 if (texture) {
464 return GrSurfaceProxy::MakeWrapped(std::move(texture));
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000465 }
466
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
468 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700469 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500471 GrSWMaskHelper helper;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000472
joshualitt8059eb92014-12-29 15:10:07 -0800473 // Set the matrix so that rendered clip elements are transformed to mask space from clip
474 // space.
475 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700476 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800477
csmartdalton3affdc12016-10-28 12:01:10 -0700478 if (!helper.init(maskSpaceIBounds, &translate)) {
479 return nullptr;
480 }
csmartdaltond211e782016-08-15 11:17:19 -0700481 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000482
csmartdalton77f2fae2016-08-08 09:55:06 -0700483 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500485 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000487
Mike Reedc1f77742016-12-09 09:00:50 -0500488 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 // Intersect and reverse difference require modifying pixels outside of the geometry
490 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
491 // but leave the pixels inside the geometry alone. For reverse difference we invert all
492 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500493 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700494 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000495 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500496 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000497 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000498 SkPath clipPath;
499 element->asPath(&clipPath);
500 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700501 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500502 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000503 continue;
504 }
505
506 // The other ops (union, xor, diff) only affect pixels inside
507 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000508 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500509 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000510 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000511 SkPath path;
512 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700513 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500514 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000515 }
516 }
517
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500518 sk_sp<GrTextureProxy> result(helper.toTexture(context, SkBackingFit::kApprox));
robertphillips391395d2016-03-02 09:26:36 -0800519
Brian Osman32342f02017-03-04 08:12:46 -0500520 GrTexture* tex = result->instantiate(context->resourceProvider());
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500521 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700522 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700523 }
robertphillips391395d2016-03-02 09:26:36 -0800524
Brian Osman32342f02017-03-04 08:12:46 -0500525 context->resourceProvider()->assignUniqueKeyToTexture(key, tex);
Brian Salomon19f0ed52017-01-06 13:54:58 -0500526 add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
Robert Phillips875218e2017-02-24 08:37:13 -0500527 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000528}