blob: a7bcce492db098b003290a8c2fa798c992b9abb7 [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;
30
csmartdaltonc6f411e2016-08-05 22:32:12 -070031bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070032 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070033 return true;
34 }
35 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
36 SkIntToScalar(fOrigin.y())));
37}
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 }
43 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
44 SkIntToScalar(fOrigin.fY)));
45}
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;
52 SkRect tempRTBounds;
53 bool origin = fOrigin.fX || fOrigin.fY;
54 if (origin) {
55 tempRTBounds = origRTBounds;
56 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
57 rtBounds = &tempRTBounds;
58 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050059 bool isAA;
60 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
61 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070062 if (origin) {
63 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
64 }
65 return true;
66 }
67 return false;
68}
69
csmartdaltonc6f411e2016-08-05 22:32:12 -070070void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
71 bool* isIntersectionOfRects) const {
72 if (!fStack) {
73 devResult->setXYWH(0, 0, width, height);
74 if (isIntersectionOfRects) {
75 *isIntersectionOfRects = true;
76 }
77 return;
78 }
79 SkRect devBounds;
80 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
81 isIntersectionOfRects);
82 devBounds.roundOut(devResult);
83}
84
bsalomon@google.com51a62862012-11-26 21:19:43 +000085////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040086// set up the draw state to enable the aa clipping mask.
bungeman06ca8ec2016-06-09 08:01:03 -070087static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
88 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000089 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -040090 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(result, domainTexels,
91 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000092}
93
robertphillips3f7357f2015-10-27 07:17:33 -070094// Does the path in 'element' require SW rendering? If so, return true (and,
95// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
96// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070097bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
98 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040099 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700100 const SkMatrix& viewMatrix,
101 const Element* element,
102 GrPathRenderer** prOut,
103 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700104 if (Element::kRect_Type == element->getType()) {
105 // rects can always be drawn directly w/o using the software path
106 // TODO: skip rrects once we're drawing them directly.
107 if (prOut) {
108 *prOut = nullptr;
109 }
110 return false;
111 } else {
112 // We shouldn't get here with an empty clip element.
113 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700114
robertphillips3f7357f2015-10-27 07:17:33 -0700115 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
116 SkPath path;
117 element->asPath(&path);
118 if (path.isInverseFillType()) {
119 path.toggleInverseFillType();
120 }
halcanary9d524f22016-03-29 09:03:52 -0700121
Brian Salomon82125e92016-12-10 09:35:48 -0500122 GrPathRendererChain::DrawType type =
123 needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
124 : GrPathRendererChain::DrawType::kColor;
halcanary9d524f22016-03-29 09:03:52 -0700125
bsalomon8acedde2016-06-24 10:42:16 -0700126 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700127 GrPathRenderer::CanDrawPathArgs canDrawArgs;
128 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
129 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700130 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500131 if (!element->isAA()) {
132 canDrawArgs.fAAType = GrAAType::kNone;
133 } else if (renderTargetContext->isUnifiedMultisampled()) {
134 canDrawArgs.fAAType = GrAAType::kMSAA;
135 } else if (renderTargetContext->isStencilBufferMultisampled()){
136 canDrawArgs.fAAType = GrAAType::kMixedSamples;
137 } else {
138 canDrawArgs.fAAType = GrAAType::kCoverage;
139 }
cdalton93a379b2016-05-11 13:58:08 -0700140 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700141
robertphillips3f7357f2015-10-27 07:17:33 -0700142 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700143 GrPathRenderer* pr =
144 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700145 if (prOut) {
146 *prOut = pr;
147 }
148 return SkToBool(!pr);
149 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000150}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000151
robertphillips@google.comfa662942012-05-17 12:20:22 +0000152/*
153 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
154 * will be used on any element. If so, it returns true to indicate that the
155 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
156 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700157bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
158 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400159 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700160 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000161 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000162 // a clip gets complex enough it can just be done in SW regardless
163 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000164
joshualitt8059eb92014-12-29 15:10:07 -0800165 // Set the matrix so that rendered clip elements are transformed to mask space from clip
166 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700167 SkMatrix translate;
168 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800169
csmartdaltonbde96c62016-08-31 12:54:46 -0700170 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000171 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700172
Mike Reedc1f77742016-12-09 09:00:50 -0500173 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700174 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700175 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500176 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700177
robertphillips59cf61a2016-07-13 09:18:21 -0700178 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400179 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700180 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000181 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000182 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000183 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000184}
185
csmartdalton77f2fae2016-08-08 09:55:06 -0700186static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700187 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700188 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700189 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700190 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700192 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700193 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700194 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700195 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700196 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500197 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 bool invert;
199 bool skip = false;
200 switch (op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500201 case kReplace_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 SkASSERT(iter.get() == elements.head());
203 // Fallthrough, handled same as intersect.
Mike Reedebfce6d2016-12-12 10:02:12 -0500204 case kIntersect_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000205 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700206 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 skip = true;
208 }
209 break;
Mike Reedebfce6d2016-12-12 10:02:12 -0500210 case kDifference_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 invert = true;
212 // We don't currently have a cheap test for whether a rect is fully outside an
213 // element's primitive, so don't attempt to set skip.
214 break;
215 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700216 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700219 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800220 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700221 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700222 return false;
bsalomona912dde2015-10-14 15:01:50 -0700223 }
joshualittb0a8a372014-09-23 09:50:21 -0700224 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700225 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000226 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700227 edgeType =
228 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000229 }
bsalomona912dde2015-10-14 15:01:50 -0700230
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 switch (iter.get()->getType()) {
232 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700233 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
234 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235 break;
236 case SkClipStack::Element::kRRect_Type: {
237 SkRRect rrect = iter.get()->getRRect();
238 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700239 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 break;
241 }
242 case SkClipStack::Element::kRect_Type: {
243 SkRect rect = iter.get()->getRect();
244 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700245 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 break;
247 }
248 default:
249 break;
250 }
bungeman06ca8ec2016-06-09 08:01:03 -0700251 if (!fps.back()) {
252 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 }
254 }
mtklein217daa72014-07-02 12:55:21 -0700255 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000256 }
257
bsalomon0b5b6b22015-10-14 08:31:34 -0700258 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700259 if (fps.count()) {
260 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000261 }
bungeman06ca8ec2016-06-09 08:01:03 -0700262 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263}
264
robertphillips@google.comf294b772012-04-27 14:29:26 +0000265////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000266// sort out what kind of clip mask needs to be created: alpha, stencil,
267// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400268bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
269 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700270 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700271 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700272 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000273
Robert Phillips784b7bf2016-12-09 13:35:02 -0500274 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(),
275 renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700276 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700277 return false;
278 }
279
csmartdaltonc6f411e2016-08-05 22:32:12 -0700280 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
281 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700282
csmartdalton77f2fae2016-08-08 09:55:06 -0700283 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700284 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500285 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000286
csmartdaltond211e782016-08-15 11:17:19 -0700287 if (reducedClip.hasIBounds() &&
288 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
289 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
290 scissorSpaceIBounds.offset(-fOrigin);
291 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700292 }
cdalton93a379b2016-05-11 13:58:08 -0700293
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700294 if (!reducedClip.windowRectangles().empty()) {
295 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
296 GrWindowRectsState::Mode::kExclusive);
297 }
298
csmartdaltond211e782016-08-15 11:17:19 -0700299 if (reducedClip.elements().isEmpty()) {
300 return InitialState::kAllIn == reducedClip.initialState();
301 }
302
csmartdalton3affdc12016-10-28 12:01:10 -0700303#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700304 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500305 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
306 renderTargetContext->height());
csmartdalton3affdc12016-10-28 12:01:10 -0700307 SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y());
308 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
309#endif
csmartdaltond211e782016-08-15 11:17:19 -0700310
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000311 // An element count of 4 was chosen because of the common pattern in Blink of:
312 // isect RR
313 // diff RR
314 // isect convex_poly
315 // isect convex_poly
316 // when drawing rounded div borders. This could probably be tuned based on a
317 // configuration's relative costs of switching RTs to generate a mask vs
318 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700319 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800320 // When there are multiple samples we want to do per-sample clipping, not compute a
321 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400322 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
323 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700324 // With a single color sample, any coverage info is lost from color once it hits the
325 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
326 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700327 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700328 }
bungeman06ca8ec2016-06-09 08:01:03 -0700329 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700330 if (reducedClip.requiresAA() &&
331 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
332 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700333 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000334 return true;
335 }
336 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000337
cdaltonede75742015-11-11 15:27:57 -0800338 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400339 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700340 sk_sp<GrTexture> result;
Brian Osman11052242016-10-27 14:47:55 -0400341 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000342 // The clip geometry is complex enough that it will be more efficient to create it
343 // entirely in software
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500344 result = CreateSoftwareClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000345 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700346 result = CreateAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 }
348
bsalomon49f085d2014-09-05 13:34:00 -0700349 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000350 // The mask's top left coord should be pinned to the rounded-out top left corner of
351 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700352 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700353 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700354 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 return true;
356 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000357 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
Robert Phillipsec2249f2016-11-09 08:54:35 -0500360 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500361 if (!rt) {
362 return true;
363 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500364
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000365 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700366 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700367 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
368 return true;
369 }
370
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700371 // This relies on the property that a reduced sub-rect of the last clip will contain all the
372 // relevant window rectangles that were in the last clip. This subtle requirement will go away
373 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700374 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
375 reducedClip.ibounds(), fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400376 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdalton7cdda992016-11-01 07:03:03 -0700377 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
378 fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700379 }
csmartdaltond211e782016-08-15 11:17:19 -0700380 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000381 return true;
382}
383
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000384////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700385// Create a 8-bit clip mask in alpha
386
387static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
388 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
389 GrUniqueKey::Builder builder(key, kDomain, 3);
390 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700391 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
392 // sometimes result in negative coordinates from clip space.
393 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
394 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700395}
396
csmartdaltonc6f411e2016-08-05 22:32:12 -0700397sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700398 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800399 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700400 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700401 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700402 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700403 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000404 }
405
Brian Osman11052242016-10-27 14:47:55 -0400406 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
407 SkBackingFit::kApprox,
408 reducedClip.width(),
409 reducedClip.height(),
410 kAlpha_8_GrPixelConfig,
411 nullptr));
412 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800413 return nullptr;
414 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000415
Brian Osman11052242016-10-27 14:47:55 -0400416 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700417 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000418 }
419
Brian Osman11052242016-10-27 14:47:55 -0400420 sk_sp<GrTexture> texture(rtc->asTexture());
Robert Phillipse60ad622016-11-17 10:22:48 -0500421 if (!texture) {
422 return nullptr;
423 }
424
robertphillipsc99b8f02016-05-15 07:53:35 -0700425 texture->resourcePriv().setUniqueKey(key);
426 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427}
428
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500429sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700430 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700431 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700432 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500433 if (GrTexture* texture = context->textureProvider()->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700434 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000435 }
436
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000437 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
438 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700439 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500441 GrSWMaskHelper helper;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000442
joshualitt8059eb92014-12-29 15:10:07 -0800443 // Set the matrix so that rendered clip elements are transformed to mask space from clip
444 // space.
445 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700446 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800447
csmartdalton3affdc12016-10-28 12:01:10 -0700448 if (!helper.init(maskSpaceIBounds, &translate)) {
449 return nullptr;
450 }
csmartdaltond211e782016-08-15 11:17:19 -0700451 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000452
csmartdalton77f2fae2016-08-08 09:55:06 -0700453 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000454 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500455 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500456 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000457
Mike Reedc1f77742016-12-09 09:00:50 -0500458 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 // Intersect and reverse difference require modifying pixels outside of the geometry
460 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
461 // but leave the pixels inside the geometry alone. For reverse difference we invert all
462 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500463 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700464 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000465 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500466 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000467 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000468 SkPath clipPath;
469 element->asPath(&clipPath);
470 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700471 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500472 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000473 continue;
474 }
475
476 // The other ops (union, xor, diff) only affect pixels inside
477 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000478 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500479 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000480 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000481 SkPath path;
482 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700483 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500484 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000485 }
486 }
487
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500488 sk_sp<GrTextureProxy> result(helper.toTexture(context, SkBackingFit::kApprox));
robertphillips391395d2016-03-02 09:26:36 -0800489
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500490 GrTexture* tex = result->instantiate(context->textureProvider());
491 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700492 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700493 }
robertphillips391395d2016-03-02 09:26:36 -0800494
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500495 tex->resourcePriv().setUniqueKey(key);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000496
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500497 return sk_ref_sp(tex);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000498}