blob: 9fc218224e4e5419b92511fd9d588ad336f29bed [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"
egdaniel8d95ffa2014-12-08 13:26:43 -080019#include "effects/GrConvexPolyEffect.h"
20#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "effects/GrTextureDomain.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050022#include "SkClipOpPriv.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000023
bsalomon@google.com8182fa02012-12-04 14:06:06 +000024typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070025typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070026typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000027
robertphillips976f5f02016-06-03 10:59:20 -070028static const int kMaxAnalyticElements = 4;
29
csmartdaltonc6f411e2016-08-05 22:32:12 -070030bool GrClipStackClip::quickContains(const SkRect& rect) const {
reed4d2cce42016-08-22 13:03:47 -070031 if (!fStack || fStack->isWideOpen()) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070032 return true;
33 }
34 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
35 SkIntToScalar(fOrigin.y())));
36}
37
bsalomon7f0d9f32016-08-15 14:49:10 -070038bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
reed4d2cce42016-08-22 13:03:47 -070039 if (!fStack || fStack->isWideOpen()) {
bsalomon7f0d9f32016-08-15 14:49:10 -070040 return true;
41 }
42 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
43 SkIntToScalar(fOrigin.fY)));
44}
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;
51 SkRect tempRTBounds;
52 bool origin = fOrigin.fX || fOrigin.fY;
53 if (origin) {
54 tempRTBounds = origRTBounds;
55 tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY));
56 rtBounds = &tempRTBounds;
57 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050058 bool isAA;
59 if (fStack->isRRect(*rtBounds, rr, &isAA)) {
60 *aa = GrBoolToAA(isAA);
bsalomoncb31e512016-08-26 10:48:19 -070061 if (origin) {
62 rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY));
63 }
64 return true;
65 }
66 return false;
67}
68
csmartdaltonc6f411e2016-08-05 22:32:12 -070069void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
70 bool* isIntersectionOfRects) const {
71 if (!fStack) {
72 devResult->setXYWH(0, 0, width, height);
73 if (isIntersectionOfRects) {
74 *isIntersectionOfRects = true;
75 }
76 return;
77 }
78 SkRect devBounds;
79 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
80 isIntersectionOfRects);
81 devBounds.roundOut(devResult);
82}
83
bsalomon@google.com51a62862012-11-26 21:19:43 +000084////////////////////////////////////////////////////////////////////////////////
Brian Salomon2ebd0c82016-10-03 17:15:28 -040085// set up the draw state to enable the aa clipping mask.
bungeman06ca8ec2016-06-09 08:01:03 -070086static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
87 const SkIRect &devBound) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000088 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -040089 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(result, domainTexels,
90 {devBound.fLeft, devBound.fTop});
robertphillips@google.coma72eef32012-05-01 17:22:59 +000091}
92
robertphillips3f7357f2015-10-27 07:17:33 -070093// Does the path in 'element' require SW rendering? If so, return true (and,
94// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
95// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070096bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
97 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -040098 const GrRenderTargetContext* renderTargetContext,
csmartdaltonc6f411e2016-08-05 22:32:12 -070099 const SkMatrix& viewMatrix,
100 const Element* element,
101 GrPathRenderer** prOut,
102 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700103 if (Element::kRect_Type == element->getType()) {
104 // rects can always be drawn directly w/o using the software path
105 // TODO: skip rrects once we're drawing them directly.
106 if (prOut) {
107 *prOut = nullptr;
108 }
109 return false;
110 } else {
111 // We shouldn't get here with an empty clip element.
112 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700113
robertphillips3f7357f2015-10-27 07:17:33 -0700114 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
115 SkPath path;
116 element->asPath(&path);
117 if (path.isInverseFillType()) {
118 path.toggleInverseFillType();
119 }
halcanary9d524f22016-03-29 09:03:52 -0700120
robertphillips3f7357f2015-10-27 07:17:33 -0700121 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700122
robertphillips423e3372015-10-27 09:23:38 -0700123 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700124 type = element->isAA()
125 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
126 : GrPathRendererChain::kStencilAndColor_DrawType;
127 } else {
128 type = element->isAA()
129 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700130 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700131 }
halcanary9d524f22016-03-29 09:03:52 -0700132
bsalomon8acedde2016-06-24 10:42:16 -0700133 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700134 GrPathRenderer::CanDrawPathArgs canDrawArgs;
135 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
136 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700137 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500138 if (!element->isAA()) {
139 canDrawArgs.fAAType = GrAAType::kNone;
140 } else if (renderTargetContext->isUnifiedMultisampled()) {
141 canDrawArgs.fAAType = GrAAType::kMSAA;
142 } else if (renderTargetContext->isStencilBufferMultisampled()){
143 canDrawArgs.fAAType = GrAAType::kMixedSamples;
144 } else {
145 canDrawArgs.fAAType = GrAAType::kCoverage;
146 }
cdalton93a379b2016-05-11 13:58:08 -0700147 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -0700148
robertphillips3f7357f2015-10-27 07:17:33 -0700149 // the 'false' parameter disallows use of the SW path renderer
csmartdaltonbde96c62016-08-31 12:54:46 -0700150 GrPathRenderer* pr =
151 context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700152 if (prOut) {
153 *prOut = pr;
154 }
155 return SkToBool(!pr);
156 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000157}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000158
robertphillips@google.comfa662942012-05-17 12:20:22 +0000159/*
160 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
161 * will be used on any element. If so, it returns true to indicate that the
162 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
163 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700164bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
165 bool hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400166 const GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700167 const GrReducedClip& reducedClip) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000168 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000169 // a clip gets complex enough it can just be done in SW regardless
170 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000171
joshualitt8059eb92014-12-29 15:10:07 -0800172 // Set the matrix so that rendered clip elements are transformed to mask space from clip
173 // space.
csmartdaltonbde96c62016-08-31 12:54:46 -0700174 SkMatrix translate;
175 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt8059eb92014-12-29 15:10:07 -0800176
csmartdaltonbde96c62016-08-31 12:54:46 -0700177 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000178 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700179
Mike Reedc1f77742016-12-09 09:00:50 -0500180 SkClipOp op = element->getOp();
robertphillips3f7357f2015-10-27 07:17:33 -0700181 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700182 bool needsStencil = invert ||
Mike Reedc1f77742016-12-09 09:00:50 -0500183 kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700184
robertphillips59cf61a2016-07-13 09:18:21 -0700185 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
Brian Osman11052242016-10-27 14:47:55 -0400186 renderTargetContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700187 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000188 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000189 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000190 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000191}
192
csmartdalton77f2fae2016-08-08 09:55:06 -0700193static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700194 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700195 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700196 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700197 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700199 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700200 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700201 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700202 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700203 while (iter.get()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500204 SkClipOp op = iter.get()->getOp();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000205 bool invert;
206 bool skip = false;
207 switch (op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500208 case kReplace_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 SkASSERT(iter.get() == elements.head());
210 // Fallthrough, handled same as intersect.
Mike Reedebfce6d2016-12-12 10:02:12 -0500211 case kIntersect_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000212 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700213 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 skip = true;
215 }
216 break;
Mike Reedebfce6d2016-12-12 10:02:12 -0500217 case kDifference_SkClipOp:
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 invert = true;
219 // We don't currently have a cheap test for whether a rect is fully outside an
220 // element's primitive, so don't attempt to set skip.
221 break;
222 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700223 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000224 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700226 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800227 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700228 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700229 return false;
bsalomona912dde2015-10-14 15:01:50 -0700230 }
joshualittb0a8a372014-09-23 09:50:21 -0700231 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700232 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700234 edgeType =
235 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000236 }
bsalomona912dde2015-10-14 15:01:50 -0700237
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 switch (iter.get()->getType()) {
239 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700240 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
241 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000242 break;
243 case SkClipStack::Element::kRRect_Type: {
244 SkRRect rrect = iter.get()->getRRect();
245 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700246 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000247 break;
248 }
249 case SkClipStack::Element::kRect_Type: {
250 SkRect rect = iter.get()->getRect();
251 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700252 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000253 break;
254 }
255 default:
256 break;
257 }
bungeman06ca8ec2016-06-09 08:01:03 -0700258 if (!fps.back()) {
259 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000260 }
261 }
mtklein217daa72014-07-02 12:55:21 -0700262 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000263 }
264
bsalomon0b5b6b22015-10-14 08:31:34 -0700265 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700266 if (fps.count()) {
267 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000268 }
bungeman06ca8ec2016-06-09 08:01:03 -0700269 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000270}
271
robertphillips@google.comf294b772012-04-27 14:29:26 +0000272////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000273// sort out what kind of clip mask needs to be created: alpha, stencil,
274// scissor, or entirely software
Brian Osman11052242016-10-27 14:47:55 -0400275bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
276 bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700277 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700278 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700279 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000280
Robert Phillips784b7bf2016-12-09 13:35:02 -0500281 SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(),
282 renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700283 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700284 return false;
285 }
286
csmartdaltonc6f411e2016-08-05 22:32:12 -0700287 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
288 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700289
csmartdalton77f2fae2016-08-08 09:55:06 -0700290 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700291 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds,
Robert Phillipsec2249f2016-11-09 08:54:35 -0500292 renderTargetContext->priv().maxWindowRectangles());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000293
csmartdaltond211e782016-08-15 11:17:19 -0700294 if (reducedClip.hasIBounds() &&
295 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
296 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
297 scissorSpaceIBounds.offset(-fOrigin);
298 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700299 }
cdalton93a379b2016-05-11 13:58:08 -0700300
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700301 if (!reducedClip.windowRectangles().empty()) {
302 out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin,
303 GrWindowRectsState::Mode::kExclusive);
304 }
305
csmartdaltond211e782016-08-15 11:17:19 -0700306 if (reducedClip.elements().isEmpty()) {
307 return InitialState::kAllIn == reducedClip.initialState();
308 }
309
csmartdalton3affdc12016-10-28 12:01:10 -0700310#ifdef SK_DEBUG
csmartdaltond211e782016-08-15 11:17:19 -0700311 SkASSERT(reducedClip.hasIBounds());
Robert Phillips784b7bf2016-12-09 13:35:02 -0500312 SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
313 renderTargetContext->height());
csmartdalton3affdc12016-10-28 12:01:10 -0700314 SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y());
315 SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
316#endif
csmartdaltond211e782016-08-15 11:17:19 -0700317
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000318 // An element count of 4 was chosen because of the common pattern in Blink of:
319 // isect RR
320 // diff RR
321 // isect convex_poly
322 // isect convex_poly
323 // when drawing rounded div borders. This could probably be tuned based on a
324 // configuration's relative costs of switching RTs to generate a mask vs
325 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700326 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800327 // When there are multiple samples we want to do per-sample clipping, not compute a
328 // fractional pixel coverage.
Brian Osman11052242016-10-27 14:47:55 -0400329 bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled();
330 if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700331 // With a single color sample, any coverage info is lost from color once it hits the
332 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
333 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700334 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700335 }
bungeman06ca8ec2016-06-09 08:01:03 -0700336 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700337 if (reducedClip.requiresAA() &&
338 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
339 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700340 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000341 return true;
342 }
343 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000344
cdaltonede75742015-11-11 15:27:57 -0800345 // If the stencil buffer is multisampled we can use it to do everything.
Brian Osman11052242016-10-27 14:47:55 -0400346 if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700347 sk_sp<GrTexture> result;
Brian Osman11052242016-10-27 14:47:55 -0400348 if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349 // The clip geometry is complex enough that it will be more efficient to create it
350 // entirely in software
csmartdaltonbde96c62016-08-31 12:54:46 -0700351 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352 } else {
csmartdaltonbde96c62016-08-31 12:54:46 -0700353 result = CreateAlphaClipMask(context, reducedClip);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 }
355
bsalomon49f085d2014-09-05 13:34:00 -0700356 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000357 // The mask's top left coord should be pinned to the rounded-out top left corner of
358 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700359 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700360 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700361 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362 return true;
363 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000364 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366
Robert Phillipsec2249f2016-11-09 08:54:35 -0500367 GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
Robert Phillipse60ad622016-11-17 10:22:48 -0500368 if (!rt) {
369 return true;
370 }
Robert Phillipsec2249f2016-11-09 08:54:35 -0500371
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000372 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdalton7cdda992016-11-01 07:03:03 -0700373 if (!context->resourceProvider()->attachStencilAttachment(rt)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700374 SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
375 return true;
376 }
377
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700378 // This relies on the property that a reduced sub-rect of the last clip will contain all the
379 // relevant window rectangles that were in the last clip. This subtle requirement will go away
380 // after clipping is overhauled.
csmartdalton7cdda992016-11-01 07:03:03 -0700381 if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
382 reducedClip.ibounds(), fOrigin)) {
Brian Osman11052242016-10-27 14:47:55 -0400383 reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin);
csmartdalton7cdda992016-11-01 07:03:03 -0700384 renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
385 fOrigin);
csmartdaltonbde96c62016-08-31 12:54:46 -0700386 }
csmartdaltond211e782016-08-15 11:17:19 -0700387 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000388 return true;
389}
390
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000391////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700392// Create a 8-bit clip mask in alpha
393
394static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
395 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
396 GrUniqueKey::Builder builder(key, kDomain, 3);
397 builder[0] = clipGenID;
csmartdalton3affdc12016-10-28 12:01:10 -0700398 // SkToS16 because image filters outset layers to a size indicated by the filter, which can
399 // sometimes result in negative coordinates from clip space.
400 builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
401 builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
bsalomon473addf2015-10-02 07:49:05 -0700402}
403
csmartdaltonc6f411e2016-08-05 22:32:12 -0700404sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdaltonbde96c62016-08-31 12:54:46 -0700405 const GrReducedClip& reducedClip) {
robertphillips391395d2016-03-02 09:26:36 -0800406 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700407 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700408 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700409 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700410 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000411 }
412
Brian Osman11052242016-10-27 14:47:55 -0400413 sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback(
414 SkBackingFit::kApprox,
415 reducedClip.width(),
416 reducedClip.height(),
417 kAlpha_8_GrPixelConfig,
418 nullptr));
419 if (!rtc) {
robertphillips391395d2016-03-02 09:26:36 -0800420 return nullptr;
421 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422
Brian Osman11052242016-10-27 14:47:55 -0400423 if (!reducedClip.drawAlphaClipMask(rtc.get())) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700424 return nullptr;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000425 }
426
Brian Osman11052242016-10-27 14:47:55 -0400427 sk_sp<GrTexture> texture(rtc->asTexture());
Robert Phillipse60ad622016-11-17 10:22:48 -0500428 if (!texture) {
429 return nullptr;
430 }
431
robertphillipsc99b8f02016-05-15 07:53:35 -0700432 texture->resourcePriv().setUniqueKey(key);
433 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434}
435
csmartdaltonc6f411e2016-08-05 22:32:12 -0700436sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdaltonbde96c62016-08-31 12:54:46 -0700437 const GrReducedClip& reducedClip) {
bsalomon473addf2015-10-02 07:49:05 -0700438 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700439 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700440 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700441 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000442 }
443
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000444 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
445 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700446 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000447
robertphillips0152d732016-05-20 06:38:43 -0700448 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000449
joshualitt8059eb92014-12-29 15:10:07 -0800450 // Set the matrix so that rendered clip elements are transformed to mask space from clip
451 // space.
452 SkMatrix translate;
csmartdaltonbde96c62016-08-31 12:54:46 -0700453 translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
joshualitt9853cce2014-11-17 14:22:48 -0800454
csmartdalton3affdc12016-10-28 12:01:10 -0700455 if (!helper.init(maskSpaceIBounds, &translate)) {
456 return nullptr;
457 }
csmartdaltond211e782016-08-15 11:17:19 -0700458 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000459
csmartdalton77f2fae2016-08-08 09:55:06 -0700460 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000461 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500462 SkClipOp op = element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500463 GrAA aa = GrBoolToAA(element->isAA());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000464
Mike Reedc1f77742016-12-09 09:00:50 -0500465 if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000466 // Intersect and reverse difference require modifying pixels outside of the geometry
467 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
468 // but leave the pixels inside the geometry alone. For reverse difference we invert all
469 // the pixels before clearing the ones outside the geometry.
Mike Reedc1f77742016-12-09 09:00:50 -0500470 if (kReverseDifference_SkClipOp == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700471 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000472 // invert the entire scene
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500473 helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000474 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000475 SkPath clipPath;
476 element->asPath(&clipPath);
477 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700478 GrShape shape(clipPath, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500479 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000480 continue;
481 }
482
483 // The other ops (union, xor, diff) only affect pixels inside
484 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000485 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500486 helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000487 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000488 SkPath path;
489 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700490 GrShape shape(path, GrStyle::SimpleFill());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500491 helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492 }
493 }
494
krajcevskiad1dc582014-06-10 15:06:47 -0700495 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800496 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700497 desc.fWidth = reducedClip.width();
498 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800499 desc.fConfig = kAlpha_8_GrPixelConfig;
500
robertphillips0152d732016-05-20 06:38:43 -0700501 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800502 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700503 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700504 }
robertphillips391395d2016-03-02 09:26:36 -0800505 result->resourcePriv().setUniqueKey(key);
506
robertphillipsc99b8f02016-05-15 07:53:35 -0700507 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000508
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000510}